Skip to content

Commit f4e77e6

Browse files
authored
[OMPIRBuilder][MLIR] Fix target-features and target-cpu handling (llvm#30)
Upstream patches to support target-features and target-cpu conflicted with some changes in ATD. This patch solves that conflict.
1 parent 1013514 commit f4e77e6

File tree

4 files changed

+21
-42
lines changed

4 files changed

+21
-42
lines changed

llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,10 @@ class OpenMPIRBuilderConfig {
103103
/// Flag for specifying if offloading is mandatory.
104104
std::optional<bool> OpenMPOffloadMandatory;
105105

106-
/// Name of the target processor.
107-
StringRef TargetCPU;
108-
/// String representation of the target processor's features.
109-
StringRef TargetFeatures;
110-
111106
/// First separator used between the initial two parts of a name.
112107
std::optional<StringRef> FirstSeparator;
113-
/// Separator used between all of the rest consecutive parts of s name
108+
109+
/// Separator used between all of the rest consecutive parts of a name
114110
std::optional<StringRef> Separator;
115111

116112
// Grid Value for the GPU target

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,14 @@ static Value *getTypeSizeInBytesValue(IRBuilder<> &Builder, Module &M,
156156
return Builder.getInt64(getTypeSizeInBytes(M, Type));
157157
}
158158

159-
static const omp::GV &getGridValue(const Triple &T, StringRef Features) {
160-
if (T.isAMDGPU()) {
161-
if (Features.count("+wavefrontsize64"))
162-
return omp::getAMDGPUGridValues<64>();
163-
return omp::getAMDGPUGridValues<32>();
164-
}
165-
if (T.isNVPTX())
166-
return omp::NVPTXGridValues;
167-
llvm_unreachable("No grid value available for this architecture!");
168-
}
169-
170159
static const omp::GV &getGridValue(const Triple &T, Function *Kernel) {
171160
if (T.isAMDGPU()) {
172161
StringRef Features =
173162
Kernel->getFnAttribute("target-features").getValueAsString();
174-
return getGridValue(T, Features);
163+
164+
if (Features.count("+wavefrontsize64"))
165+
return omp::getAMDGPUGridValues<64>();
166+
return omp::getAMDGPUGridValues<32>();
175167
}
176168
if (T.isNVPTX())
177169
return omp::NVPTXGridValues;
@@ -5639,7 +5631,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTargetInit(
56395631
Function *Kernel = Builder.GetInsertBlock()->getParent();
56405632

56415633
// Set the grid value in the config needed for lowering later on
5642-
Config.setGridValue(getGridValue(T, Config.TargetFeatures));
5634+
Config.setGridValue(getGridValue(T, Kernel));
56435635

56445636
// Manifest the launch configuration in the metadata matching the kernel
56455637
// environment.
@@ -5897,11 +5889,6 @@ void OpenMPIRBuilder::setOutlinedTargetRegionFunctionAttributes(
58975889
if (T.isAMDGCN())
58985890
OutlinedFn->setCallingConv(CallingConv::AMDGPU_KERNEL);
58995891
}
5900-
5901-
if (!Config.TargetCPU.empty())
5902-
OutlinedFn->addFnAttr("target-cpu", Config.TargetCPU);
5903-
if (!Config.TargetFeatures.empty())
5904-
OutlinedFn->addFnAttr("target-features", Config.TargetFeatures);
59055892
}
59065893

59075894
Constant *OpenMPIRBuilder::createOutlinedFunctionID(Function *OutlinedFn,
@@ -6250,6 +6237,18 @@ static Function *createOutlinedFunction(
62506237
auto Func = Function::Create(FuncType, GlobalValue::InternalLinkage, FuncName,
62516238
Builder.GetInsertBlock()->getModule());
62526239

6240+
// Forward target-cpu and target-features function attributes from the
6241+
// original function to the new outlined function.
6242+
Function *ParentFn = Builder.GetInsertBlock()->getParent();
6243+
6244+
auto TargetCpuAttr = ParentFn->getFnAttribute("target-cpu");
6245+
if (TargetCpuAttr.isStringAttribute())
6246+
Func->addFnAttr(TargetCpuAttr);
6247+
6248+
auto TargetFeaturesAttr = ParentFn->getFnAttribute("target-features");
6249+
if (TargetFeaturesAttr.isStringAttribute())
6250+
Func->addFnAttr(TargetFeaturesAttr);
6251+
62536252
if (OMPBuilder.Config.isTargetDevice()) {
62546253
std::vector<llvm::WeakTrackingVH> LLVMCompilerUsed;
62556254
emitExecutionMode(OMPBuilder, Builder, FuncName, false, LLVMCompilerUsed);

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,22 +2839,6 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
28392839
using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
28402840
auto bodyCB = [&](InsertPointTy allocaIP,
28412841
InsertPointTy codeGenIP) -> InsertPointTy {
2842-
// Forward target-cpu and target-features function attributes from the
2843-
// original function to the new outlined function.
2844-
llvm::Function *llvmParentFn =
2845-
moduleTranslation.lookupFunction(parentFn.getName());
2846-
llvm::Function *llvmOutlinedFn = codeGenIP.getBlock()->getParent();
2847-
assert(llvmParentFn && llvmOutlinedFn &&
2848-
"Both parent and outlined functions must exist at this point");
2849-
2850-
if (auto attr = llvmParentFn->getFnAttribute("target-cpu");
2851-
attr.isStringAttribute())
2852-
llvmOutlinedFn->addFnAttr(attr);
2853-
2854-
if (auto attr = llvmParentFn->getFnAttribute("target-features");
2855-
attr.isStringAttribute())
2856-
llvmOutlinedFn->addFnAttr(attr);
2857-
28582842
builder.restoreIP(codeGenIP);
28592843
unsigned argIndex = 0;
28602844
for (auto &mapOp : mapOperands) {

mlir/test/Target/LLVMIR/omptarget-parallel-wsloop.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<"dlti.alloca_memo
3939

4040
// CHECK: define internal void @[[LOOP_BODY_FUNC]](i32 %[[CNT:.*]], ptr %[[LOOP_BODY_ARG_PTR:.*]]) #[[ATTRS2:[0-9]+]] {
4141

42-
// CHECK: attributes #[[ATTRS2:.*]] = {
42+
// CHECK: attributes #[[ATTRS1]] = {
4343
// CHECK-SAME: "target-cpu"="gfx90a"
4444
// CHECK-SAME: "target-features"="+gfx9-insts,+wavefrontsize64"
45-
// CHECK: attributes #[[ATTRS1:.*]] = {
45+
// CHECK: attributes #[[ATTRS2]] = {
4646
// CHECK-SAME: "target-cpu"="gfx90a"
4747
// CHECK-SAME: "target-features"="+gfx9-insts,+wavefrontsize64"

0 commit comments

Comments
 (0)