[clang] [CIR][SYCL] Emit sycl-device module flag for SYCL device Compliation (PR #238117)
Balaji V. Iyer. via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 2 13:56:05 PST 2026
https://github.com/bviyer updated https://github.com/llvm/llvm-project/pull/238117
>From c3dadb2efec7858b3d95057ac3ec44424eb82c10 Mon Sep 17 00:00:00 2001
From: "Balaji V. Iyer" <balaji.iyer at intel.com>
Date: Mon, 2 Nov 2026 13:43:10 -0800
Subject: [PATCH 1/2] Init commit
---
clang/lib/CIR/CodeGen/CIRGenModule.cpp | 3 +++
clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp | 8 ++++++++
2 files changed, 11 insertions(+)
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index 11904c58f82350..3a3a80935f0404 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -164,6 +164,9 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
spirMajor > 1 ? 0 : 2));
}
}
+ if (langOpts.SYCLIsDevice)
+ getModule()->setAttr("cir.sycl_device", builder.getI32IntegerAttr(1));
+
getModule()->setAttr(cir::CIRDialect::getTripleAttrName(),
builder.getStringAttr(getTriple().str()));
getModule()->setAttr(cir::CIRDialect::getTargetABIAttrName(),
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp
index 2b80606fea6138..78e1fc86d9079b 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp
@@ -219,6 +219,14 @@ class CIRDialectLLVMIRTranslationInterface
}
}
+ if (attribute.getName() == "cir.sycl_device") {
+ if (auto intAttr =
+ mlir::dyn_cast<mlir::IntegerAttr>(attribute.getValue())) {
+ llvmModule->addModuleFlag(llvm::Module::Error, "sycl-device",
+ static_cast<uint32_t>(intAttr.getInt()));
+ }
+ }
+
return mlir::success();
}
};
>From 97d4a935a018a03ba044050e24763dadc1d36a19 Mon Sep 17 00:00:00 2001
From: "Balaji V. Iyer" <balaji.iyer at intel.com>
Date: Mon, 2 Nov 2026 13:55:36 -0800
Subject: [PATCH 2/2] Add test case.
---
.../CodeGenSYCL/sycl-device-module-flag.cpp | 21 +++++++++++++++++++
1 file changed, 21 insertions(+)
create mode 100644 clang/test/CIR/CodeGenSYCL/sycl-device-module-flag.cpp
diff --git a/clang/test/CIR/CodeGenSYCL/sycl-device-module-flag.cpp b/clang/test/CIR/CodeGenSYCL/sycl-device-module-flag.cpp
new file mode 100644
index 00000000000000..de56f2b5f1825b
--- /dev/null
+++ b/clang/test/CIR/CodeGenSYCL/sycl-device-module-flag.cpp
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -fsycl-is-device -triple spir64 -fclangir -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -fsycl-is-device -triple spir64 -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -fsycl-is-host -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefix=HOST
+// RUN: %clang_cc1 -fsycl-is-host -emit-llvm %s -o - | FileCheck %s --check-prefix=HOST
+
+// This test checks that the sycl-device module flag is created for device
+// compilations and not for host compilations.
+
+template <typename name, typename Func>
+__attribute__((sycl_kernel)) void kernel_single_task(const Func &kernelFunc) {
+ kernelFunc();
+}
+
+void foo() {
+ kernel_single_task<class kernel_function>([]() {});
+}
+
+// Check for the presence of the sycl-device module flag in device
+// compilations and its absence in host compilations.
+// CHECK: !{{[0-9]*}} = !{i32 1, !"sycl-device", i32 1}
+// HOST-NOT: !"sycl-device"
More information about the cfe-commits
mailing list