Skip to content

Commit d0b641b

Browse files
authored
[OMPIRBuilder] Propagate attributes to outlined target regions (llvm#117875)
This patch copies the target-cpu and target-features attributes of functions containing target regions into the corresponding outlined function holding the target region. This mirrors what is currently being done for all other outlined functions through the `CodeExtractor` in `OpenMPIRBuilder::finalize()`.
1 parent fabc443 commit d0b641b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6774,6 +6774,18 @@ static Expected<Function *> createOutlinedFunction(
67746774
auto Func =
67756775
Function::Create(FuncType, GlobalValue::InternalLinkage, FuncName, M);
67766776

6777+
// Forward target-cpu and target-features function attributes from the
6778+
// original function to the new outlined function.
6779+
Function *ParentFn = Builder.GetInsertBlock()->getParent();
6780+
6781+
auto TargetCpuAttr = ParentFn->getFnAttribute("target-cpu");
6782+
if (TargetCpuAttr.isStringAttribute())
6783+
Func->addFnAttr(TargetCpuAttr);
6784+
6785+
auto TargetFeaturesAttr = ParentFn->getFnAttribute("target-features");
6786+
if (TargetFeaturesAttr.isStringAttribute())
6787+
Func->addFnAttr(TargetFeaturesAttr);
6788+
67776789
if (OMPBuilder.Config.isTargetDevice()) {
67786790
Value *ExecMode =
67796791
OMPBuilder.emitKernelExecutionMode(FuncName, DefaultAttrs.ExecFlags);

llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6169,6 +6169,8 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
61696169
OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
61706170
OMPBuilder.setConfig(Config);
61716171
F->setName("func");
6172+
F->addFnAttr("target-cpu", "x86-64");
6173+
F->addFnAttr("target-features", "+mmx,+sse");
61726174
IRBuilder<> Builder(BB);
61736175
auto *Int32Ty = Builder.getInt32Ty();
61746176

@@ -6320,6 +6322,13 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
63206322
StringRef FunctionName2 = OutlinedFunc->getName();
63216323
EXPECT_TRUE(FunctionName2.starts_with("__omp_offloading"));
63226324

6325+
// Check that target-cpu and target-features were propagated to the outlined
6326+
// function
6327+
EXPECT_EQ(OutlinedFunc->getFnAttribute("target-cpu"),
6328+
F->getFnAttribute("target-cpu"));
6329+
EXPECT_EQ(OutlinedFunc->getFnAttribute("target-features"),
6330+
F->getFnAttribute("target-features"));
6331+
63236332
EXPECT_FALSE(verifyModule(*M, &errs()));
63246333
}
63256334

@@ -6330,6 +6339,8 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
63306339
OMPBuilder.initialize();
63316340

63326341
F->setName("func");
6342+
F->addFnAttr("target-cpu", "gfx90a");
6343+
F->addFnAttr("target-features", "+gfx9-insts,+wavefrontsize64");
63336344
IRBuilder<> Builder(BB);
63346345
OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
63356346

@@ -6407,6 +6418,13 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
64076418
Function *OutlinedFn = TargetStore->getFunction();
64086419
EXPECT_NE(F, OutlinedFn);
64096420

6421+
// Check that target-cpu and target-features were propagated to the outlined
6422+
// function
6423+
EXPECT_EQ(OutlinedFn->getFnAttribute("target-cpu"),
6424+
F->getFnAttribute("target-cpu"));
6425+
EXPECT_EQ(OutlinedFn->getFnAttribute("target-features"),
6426+
F->getFnAttribute("target-features"));
6427+
64106428
EXPECT_TRUE(OutlinedFn->hasWeakODRLinkage());
64116429
// Account for the "implicit" first argument.
64126430
EXPECT_EQ(OutlinedFn->getName(), "__omp_offloading_1_2_parent_l3");
@@ -6657,6 +6675,13 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDeviceSPMD) {
66576675
EXPECT_NE(OutlinedFn, nullptr);
66586676
EXPECT_NE(F, OutlinedFn);
66596677

6678+
// Check that target-cpu and target-features were propagated to the outlined
6679+
// function
6680+
EXPECT_EQ(OutlinedFn->getFnAttribute("target-cpu"),
6681+
F->getFnAttribute("target-cpu"));
6682+
EXPECT_EQ(OutlinedFn->getFnAttribute("target-features"),
6683+
F->getFnAttribute("target-features"));
6684+
66606685
EXPECT_TRUE(OutlinedFn->hasWeakODRLinkage());
66616686
// Account for the "implicit" first argument.
66626687
EXPECT_EQ(OutlinedFn->getName(), "__omp_offloading_1_2_parent_l3");

0 commit comments

Comments
 (0)