Author: Yaxun (Sam) Liu
Date: 2021-06-25T10:01:51-04:00
New Revision: 3193133add7eeeaa3872c78aa959bacdc08e59d9

URL: 
https://github.com/llvm/llvm-project/commit/3193133add7eeeaa3872c78aa959bacdc08e59d9
DIFF: 
https://github.com/llvm/llvm-project/commit/3193133add7eeeaa3872c78aa959bacdc08e59d9.diff

LOG: [OpenCL] Do not include default header for preprocessor output as input

When clang driver is used with -save-temps to compile OpenCL program,
clang driver first launches clang -cc1 -E to generate preprocessor expansion 
output,
then launches clang -cc1 with the generated preprocessor expansion output as 
input
to generate LLVM IR.

Currently clang by default passes "-finclude-default-header" 
"-fdeclare-opencl-builtins"
in both steps, which causes default header included again in the second step, 
which
causes error.

This patch let clang not to include default header when input type is 
preprocessor expansion
output, which fixes the issue.

Reviewed by: Anastasia Stulova

Differential Revision: https://reviews.llvm.org/D104800

Added: 
    

Modified: 
    clang/lib/Driver/ToolChains/Clang.cpp
    clang/test/Driver/amdgpu-toolchain-opencl.cl

Removed: 
    


################################################################################
diff  --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index f1b5e25525979..a3f0ec577379e 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3286,7 +3286,8 @@ static void RenderOpenCLOptions(const ArgList &Args, 
ArgStringList &CmdArgs,
       CmdArgs.push_back(Args.MakeArgString(A->getOption().getPrefixedName()));
 
   // Only add the default headers if we are compiling OpenCL sources.
-  if ((types::isOpenCL(InputType) || Args.hasArg(options::OPT_cl_std_EQ)) &&
+  if ((types::isOpenCL(InputType) ||
+       (Args.hasArg(options::OPT_cl_std_EQ) && types::isSrcFile(InputType))) &&
       !Args.hasArg(options::OPT_cl_no_stdinc)) {
     CmdArgs.push_back("-finclude-default-header");
     CmdArgs.push_back("-fdeclare-opencl-builtins");

diff  --git a/clang/test/Driver/amdgpu-toolchain-opencl.cl 
b/clang/test/Driver/amdgpu-toolchain-opencl.cl
index 3994387f3eadb..152eda1c46927 100644
--- a/clang/test/Driver/amdgpu-toolchain-opencl.cl
+++ b/clang/test/Driver/amdgpu-toolchain-opencl.cl
@@ -7,6 +7,12 @@
 // RUN: %clang -### -target amdgcn-amd-amdhsa-opencl -x cl -c -emit-llvm 
-mcpu=fiji -Og %s 2>&1 | FileCheck -check-prefix=CHECK_Og %s
 // RUN: %clang -### -target amdgcn-amd-amdhsa-opencl -x cl -c -emit-llvm 
-mcpu=fiji -Ofast %s 2>&1 | FileCheck -check-prefix=CHECK_Ofast %s
 // RUN: %clang -### -target amdgcn-amd-amdhsa-opencl -x cl -c -emit-llvm 
-mcpu=fiji %s 2>&1 | FileCheck -check-prefix=CHECK_O_DEFAULT %s
+
+// Check default include file is not included for preprocessor output.
+
+// RUN: %clang -### -target amdgcn-amd-amdhsa-opencl -x cl -c -emit-llvm 
-mcpu=fiji %s 2>&1 | FileCheck -check-prefix=CHK-INC %s
+// RUN: %clang -### -target amdgcn-amd-amdhsa-opencl -x cl -c -emit-llvm 
-mcpu=fiji -save-temps %s 2>&1 | FileCheck -check-prefix=CHK-INC %s
+
 // CHECK_O0: clang{{.*}} "-O0"
 // CHECK_O1: clang{{.*}} "-O1"
 // CHECK_O2: clang{{.*}} "-O2"
@@ -17,3 +23,5 @@
 // CHECK_Ofast: {{.*}}clang{{.*}} "-Ofast"
 // CHECK_O_DEFAULT: clang{{.*}} "-O3"
 
+// CHK-INC: clang{{.*}} "-cc1" {{.*}}"-finclude-default-header" 
"-fdeclare-opencl-builtins" {{.*}}"-x" "cl"
+// CHK-INC-NOT: clang{{.*}} "-cc1" {{.*}}"-finclude-default-header" 
"-fdeclare-opencl-builtins" {{.*}}"-x" "cpp-output"


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to