[clang] c476a4a - [Clang][OpenCL] Fix Missing `-fdeclare-opencl-builtins` When Using `--save-temps` (#131017)

via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 12 14:00:22 PDT 2025


Author: Shilei Tian
Date: 2025-03-12T17:00:19-04:00
New Revision: c476a4a907a51371f051fd83987aaeb2c4a011af

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

LOG: [Clang][OpenCL] Fix Missing `-fdeclare-opencl-builtins` When Using `--save-temps` (#131017)

When compiling an OpenCL program directly with `clang` using
`--save-temps`, an
error may occur if the program contains OpenCL builtins:

```
test.cl:3:21: error: use of undeclared identifier 'get_global_id'
    3 |   unsigned int id = get_global_id(0);
      |                     ^
```

This happens because the driver does not add `-fdeclare-opencl-builtins`
when
the input type is `TY_PP_CL`. This PR fixes the issue.

Added: 
    clang/test/Driver/opencl-aot-compilation.cl

Modified: 
    clang/lib/Driver/Types.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Driver/Types.cpp b/clang/lib/Driver/Types.cpp
index eaca74a7b5529..08866e89ea447 100644
--- a/clang/lib/Driver/Types.cpp
+++ b/clang/lib/Driver/Types.cpp
@@ -226,7 +226,17 @@ bool types::isObjC(ID Id) {
   }
 }
 
-bool types::isOpenCL(ID Id) { return Id == TY_CL || Id == TY_CLCXX; }
+bool types::isOpenCL(ID Id) {
+  switch (Id) {
+  default:
+    return false;
+  case TY_PP_CL:
+  case TY_PP_CLCXX:
+  case TY_CL:
+  case TY_CLCXX:
+    return true;
+  }
+}
 
 bool types::isCXX(ID Id) {
   switch (Id) {

diff  --git a/clang/test/Driver/opencl-aot-compilation.cl b/clang/test/Driver/opencl-aot-compilation.cl
new file mode 100644
index 0000000000000..eba658619b7f8
--- /dev/null
+++ b/clang/test/Driver/opencl-aot-compilation.cl
@@ -0,0 +1,4 @@
+// RUN: %clang -x cl %s --save-temps -### 2>&1 | FileCheck %s
+
+// CHECK: "-fdeclare-opencl-builtins" {{.*}} "[[SRC:.+]].cli" "-x" "cl" "{{.*}}[[SRC]].cl"
+// CHECK-NEXT: "-fdeclare-opencl-builtins" {{.*}} "-x" "cl-cpp-output" "[[SRC]].cli"


        


More information about the cfe-commits mailing list