diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h index ddc1f744a495a..5d53c980bbb99 100644 --- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h +++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h @@ -103,14 +103,10 @@ class OpenMPIRBuilderConfig { /// Flag for specifying if offloading is mandatory. std::optional OpenMPOffloadMandatory; - /// Name of the target processor. - StringRef TargetCPU; - /// String representation of the target processor's features. - StringRef TargetFeatures; - /// First separator used between the initial two parts of a name. std::optional FirstSeparator; - /// Separator used between all of the rest consecutive parts of s name + + /// Separator used between all of the rest consecutive parts of a name std::optional Separator; // Grid Value for the GPU target diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp index 3b71893b8a419..07a9d448ed022 100644 --- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp +++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp @@ -156,22 +156,14 @@ static Value *getTypeSizeInBytesValue(IRBuilder<> &Builder, Module &M, return Builder.getInt64(getTypeSizeInBytes(M, Type)); } -static const omp::GV &getGridValue(const Triple &T, StringRef Features) { - if (T.isAMDGPU()) { - if (Features.count("+wavefrontsize64")) - return omp::getAMDGPUGridValues<64>(); - return omp::getAMDGPUGridValues<32>(); - } - if (T.isNVPTX()) - return omp::NVPTXGridValues; - llvm_unreachable("No grid value available for this architecture!"); -} - static const omp::GV &getGridValue(const Triple &T, Function *Kernel) { if (T.isAMDGPU()) { StringRef Features = Kernel->getFnAttribute("target-features").getValueAsString(); - return getGridValue(T, Features); + + if (Features.count("+wavefrontsize64")) + return omp::getAMDGPUGridValues<64>(); + return omp::getAMDGPUGridValues<32>(); } if (T.isNVPTX()) return omp::NVPTXGridValues; @@ -5638,7 +5630,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTargetInit( Function *Kernel = Builder.GetInsertBlock()->getParent(); // Set the grid value in the config needed for lowering later on - Config.setGridValue(getGridValue(T, Config.TargetFeatures)); + Config.setGridValue(getGridValue(T, Kernel)); // Manifest the launch configuration in the metadata matching the kernel // environment. @@ -5896,11 +5888,6 @@ void OpenMPIRBuilder::setOutlinedTargetRegionFunctionAttributes( if (T.isAMDGCN()) OutlinedFn->setCallingConv(CallingConv::AMDGPU_KERNEL); } - - if (!Config.TargetCPU.empty()) - OutlinedFn->addFnAttr("target-cpu", Config.TargetCPU); - if (!Config.TargetFeatures.empty()) - OutlinedFn->addFnAttr("target-features", Config.TargetFeatures); } Constant *OpenMPIRBuilder::createOutlinedFunctionID(Function *OutlinedFn, @@ -6249,6 +6236,18 @@ static Function *createOutlinedFunction( auto Func = Function::Create(FuncType, GlobalValue::InternalLinkage, FuncName, Builder.GetInsertBlock()->getModule()); + // Forward target-cpu and target-features function attributes from the + // original function to the new outlined function. + Function *ParentFn = Builder.GetInsertBlock()->getParent(); + + auto TargetCpuAttr = ParentFn->getFnAttribute("target-cpu"); + if (TargetCpuAttr.isStringAttribute()) + Func->addFnAttr(TargetCpuAttr); + + auto TargetFeaturesAttr = ParentFn->getFnAttribute("target-features"); + if (TargetFeaturesAttr.isStringAttribute()) + Func->addFnAttr(TargetFeaturesAttr); + if (OMPBuilder.Config.isTargetDevice()) { std::vector LLVMCompilerUsed; emitExecutionMode(OMPBuilder, Builder, FuncName, false, LLVMCompilerUsed); diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp index 6a6c95ddf68d1..2003c7bfd33c4 100644 --- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp @@ -2840,22 +2840,6 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder, using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; auto bodyCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP) -> InsertPointTy { - // Forward target-cpu and target-features function attributes from the - // original function to the new outlined function. - llvm::Function *llvmParentFn = - moduleTranslation.lookupFunction(parentFn.getName()); - llvm::Function *llvmOutlinedFn = codeGenIP.getBlock()->getParent(); - assert(llvmParentFn && llvmOutlinedFn && - "Both parent and outlined functions must exist at this point"); - - if (auto attr = llvmParentFn->getFnAttribute("target-cpu"); - attr.isStringAttribute()) - llvmOutlinedFn->addFnAttr(attr); - - if (auto attr = llvmParentFn->getFnAttribute("target-features"); - attr.isStringAttribute()) - llvmOutlinedFn->addFnAttr(attr); - builder.restoreIP(codeGenIP); unsigned argIndex = 0; for (auto &mapOp : mapOperands) { diff --git a/mlir/test/Target/LLVMIR/omptarget-parallel-wsloop.mlir b/mlir/test/Target/LLVMIR/omptarget-parallel-wsloop.mlir index 09db9d3323e6c..afbf5f2224630 100644 --- a/mlir/test/Target/LLVMIR/omptarget-parallel-wsloop.mlir +++ b/mlir/test/Target/LLVMIR/omptarget-parallel-wsloop.mlir @@ -39,9 +39,9 @@ module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<"dlti.alloca_memo // CHECK: define internal void @[[LOOP_BODY_FUNC]](i32 %[[CNT:.*]], ptr %[[LOOP_BODY_ARG_PTR:.*]]) #[[ATTRS2:[0-9]+]] { -// CHECK: attributes #[[ATTRS2:.*]] = { +// CHECK: attributes #[[ATTRS1]] = { // CHECK-SAME: "target-cpu"="gfx90a" // CHECK-SAME: "target-features"="+gfx9-insts,+wavefrontsize64" -// CHECK: attributes #[[ATTRS1:.*]] = { +// CHECK: attributes #[[ATTRS2]] = { // CHECK-SAME: "target-cpu"="gfx90a" // CHECK-SAME: "target-features"="+gfx9-insts,+wavefrontsize64"