Author: Jan Leyonberg
Date: 2026-03-31T12:52:41-04:00
New Revision: e04f8bb754bb783279822cb77ff3505c073b7b1d

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

LOG: [CIR] Add OMP module attributes to MLIR module (#189394)

This patch adds OpenMP attributes to the MLIR module op used to
communicate various settings e.g. thread options and offloading
information.

Co-authored-by: Claude Opus 4.6 <[email protected]>

Added: 
    clang/test/CIR/CodeGenOpenMP/omp-module-attrs.c

Modified: 
    clang/lib/CIR/CodeGen/CIRGenModule.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp 
b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index 40fc0bfaf7eb6..2bc33c191bb32 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -16,6 +16,7 @@
 #include "CIRGenConstantEmitter.h"
 #include "CIRGenFunction.h"
 
+#include "mlir/Dialect/OpenMP/OpenMPOffloadUtils.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/ASTLambda.h"
 #include "clang/AST/Attrs.inc"
@@ -135,6 +136,16 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
                                              cgo.OptimizationLevel,
                                              cgo.OptimizeSize));
 
+  if (langOpts.OpenMP) {
+    mlir::omp::OffloadModuleOpts ompOpts(
+        langOpts.OpenMPTargetDebug, langOpts.OpenMPTeamSubscription,
+        langOpts.OpenMPThreadSubscription, langOpts.OpenMPNoThreadState,
+        langOpts.OpenMPNoNestedParallelism, langOpts.OpenMPIsTargetDevice,
+        getTriple().isGPU(), langOpts.OpenMPForceUSM, langOpts.OpenMP,
+        langOpts.OMPHostIRFile, langOpts.OMPTargetTriples, langOpts.NoGPULib);
+    mlir::omp::setOffloadModuleInterfaceAttributes(theModule, ompOpts);
+  }
+
   if (langOpts.CUDA)
     createCUDARuntime();
 

diff  --git a/clang/test/CIR/CodeGenOpenMP/omp-module-attrs.c 
b/clang/test/CIR/CodeGenOpenMP/omp-module-attrs.c
new file mode 100644
index 0000000000000..833268b02d312
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenMP/omp-module-attrs.c
@@ -0,0 +1,68 @@
+// Test OpenMP module attributes in CIR output.
+
+// Host, x86_64, no target triples
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -fclangir 
-emit-cir %s -o - \
+// RUN:   | FileCheck %s --check-prefix=HOST
+
+// HOST: module {{.*}} attributes {
+// HOST-SAME: omp.is_gpu = false
+// HOST-SAME: omp.is_target_device = false
+
+// Host with target triples
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -fclangir 
-emit-cir \
+// RUN:   -fopenmp-targets=amdgcn-amd-amdhsa,nvptx64-nvidia-cuda %s -o - \
+// RUN:   | FileCheck %s --check-prefix=HOST-TRIPLES
+
+// HOST-TRIPLES: module {{.*}} attributes {
+// HOST-TRIPLES-SAME: omp.is_gpu = false
+// HOST-TRIPLES-SAME: omp.is_target_device = false
+// HOST-TRIPLES-SAME: omp.target_triples = ["amdgcn-amd-amdhsa", 
"nvptx64-nvidia-cuda"]
+
+// Device, AMDGPU
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fopenmp -fclangir -emit-cir \
+// RUN:   -fopenmp-is-target-device %s -o - \
+// RUN:   | FileCheck %s --check-prefix=AMDGPU-DEVICE
+
+// AMDGPU-DEVICE: module {{.*}} attributes {
+// AMDGPU-DEVICE-SAME: omp.is_gpu = true
+// AMDGPU-DEVICE-SAME: omp.is_target_device = true
+
+// Device, NVPTX
+// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fopenmp -fclangir -emit-cir \
+// RUN:   -fopenmp-is-target-device %s -o - \
+// RUN:   | FileCheck %s --check-prefix=NVPTX-DEVICE
+
+// NVPTX-DEVICE: module {{.*}} attributes {
+// NVPTX-DEVICE-SAME: omp.is_gpu = true
+// NVPTX-DEVICE-SAME: omp.is_target_device = true
+
+// Device, CPU
+// RUN: %clang_cc1 -fopenmp -fclangir -emit-cir \
+// RUN:   -fopenmp-is-target-device %s -o - \
+// RUN:   | FileCheck %s --check-prefix=CPU-DEVICE
+
+// CPU-DEVICE: module {{.*}} attributes {
+// CPU-DEVICE-SAME: omp.is_gpu = false
+// CPU-DEVICE-SAME: omp.is_target_device = true
+
+// Device with omp.flags
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fopenmp -fclangir -emit-cir \
+// RUN:   -fopenmp-is-target-device \
+// RUN:   -fopenmp-assume-no-thread-state \
+// RUN:   -fopenmp-assume-no-nested-parallelism %s -o - \
+// RUN:   | FileCheck %s --check-prefix=DEVICE-FLAGS
+
+// DEVICE-FLAGS: module {{.*}}  attributes {
+// DEVICE-FLAGS-SAME: omp.flags = #omp.flags<assume_no_thread_state = true, 
assume_no_nested_parallelism = true
+// DEVICE-FLAGS-SAME: omp.is_gpu = true
+// DEVICE-FLAGS-SAME: omp.is_target_device = true
+
+// Force USM (host)
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -fclangir 
-emit-cir \
+// RUN:   -fopenmp-force-usm %s -o - \
+// RUN:   | FileCheck %s --check-prefix=USM
+
+// USM: module {{.*}}  attributes {
+// USM-SAME: omp.requires = #omp<clause_requires unified_shared_memory>
+
+void omp_function(void) {}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to