Skip to content

Commit 7de5d58

Browse files
committed
[OMPIRBuilder] Propagate attributes to outlined target regions
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 b1e4eb5 commit 7de5d58

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
@@ -6773,6 +6773,18 @@ static Expected<Function *> createOutlinedFunction(
67736773
auto Func =
67746774
Function::Create(FuncType, GlobalValue::InternalLinkage, FuncName, M);
67756775

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

llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6122,6 +6122,8 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
61226122
OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
61236123
OMPBuilder.setConfig(Config);
61246124
F->setName("func");
6125+
F->addFnAttr("target-cpu", "x86-64");
6126+
F->addFnAttr("target-features", "+mmx,+sse");
61256127
IRBuilder<> Builder(BB);
61266128
auto *Int32Ty = Builder.getInt32Ty();
61276129

@@ -6270,6 +6272,13 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
62706272
StringRef FunctionName2 = OutlinedFunc->getName();
62716273
EXPECT_TRUE(FunctionName2.starts_with("__omp_offloading"));
62726274

6275+
// Check that target-cpu and target-features were propagated to the outlined
6276+
// function
6277+
EXPECT_EQ(OutlinedFunc->getFnAttribute("target-cpu"),
6278+
F->getFnAttribute("target-cpu"));
6279+
EXPECT_EQ(OutlinedFunc->getFnAttribute("target-features"),
6280+
F->getFnAttribute("target-features"));
6281+
62736282
EXPECT_FALSE(verifyModule(*M, &errs()));
62746283
}
62756284

@@ -6280,6 +6289,8 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
62806289
OMPBuilder.initialize();
62816290

62826291
F->setName("func");
6292+
F->addFnAttr("target-cpu", "gfx90a");
6293+
F->addFnAttr("target-features", "+gfx9-insts,+wavefrontsize64");
62836294
IRBuilder<> Builder(BB);
62846295
OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
62856296

@@ -6357,6 +6368,13 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
63576368
Function *OutlinedFn = TargetStore->getFunction();
63586369
EXPECT_NE(F, OutlinedFn);
63596370

6371+
// Check that target-cpu and target-features were propagated to the outlined
6372+
// function
6373+
EXPECT_EQ(OutlinedFn->getFnAttribute("target-cpu"),
6374+
F->getFnAttribute("target-cpu"));
6375+
EXPECT_EQ(OutlinedFn->getFnAttribute("target-features"),
6376+
F->getFnAttribute("target-features"));
6377+
63606378
EXPECT_TRUE(OutlinedFn->hasWeakODRLinkage());
63616379
// Account for the "implicit" first argument.
63626380
EXPECT_EQ(OutlinedFn->getName(), "__omp_offloading_1_2_parent_l3");
@@ -6598,6 +6616,13 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDeviceSPMD) {
65986616
EXPECT_NE(OutlinedFn, nullptr);
65996617
EXPECT_NE(F, OutlinedFn);
66006618

6619+
// Check that target-cpu and target-features were propagated to the outlined
6620+
// function
6621+
EXPECT_EQ(OutlinedFn->getFnAttribute("target-cpu"),
6622+
F->getFnAttribute("target-cpu"));
6623+
EXPECT_EQ(OutlinedFn->getFnAttribute("target-features"),
6624+
F->getFnAttribute("target-features"));
6625+
66016626
EXPECT_TRUE(OutlinedFn->hasWeakODRLinkage());
66026627
// Account for the "implicit" first argument.
66036628
EXPECT_EQ(OutlinedFn->getName(), "__omp_offloading_1_2_parent_l3");

0 commit comments

Comments
 (0)