Skip to content

Commit ab3e0d2

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 47a6495 commit ab3e0d2

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
@@ -6787,6 +6787,18 @@ static Expected<Function *> createOutlinedFunction(
67876787
auto Func =
67886788
Function::Create(FuncType, GlobalValue::InternalLinkage, FuncName, M);
67896789

6790+
// Forward target-cpu and target-features function attributes from the
6791+
// original function to the new outlined function.
6792+
Function *ParentFn = Builder.GetInsertBlock()->getParent();
6793+
6794+
auto TargetCpuAttr = ParentFn->getFnAttribute("target-cpu");
6795+
if (TargetCpuAttr.isStringAttribute())
6796+
Func->addFnAttr(TargetCpuAttr);
6797+
6798+
auto TargetFeaturesAttr = ParentFn->getFnAttribute("target-features");
6799+
if (TargetFeaturesAttr.isStringAttribute())
6800+
Func->addFnAttr(TargetFeaturesAttr);
6801+
67906802
if (OMPBuilder.Config.isTargetDevice()) {
67916803
Value *ExecMode =
67926804
OMPBuilder.emitKernelExecutionMode(FuncName, DefaultAttrs.ExecFlags);

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

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

6370+
// Check that target-cpu and target-features were propagated to the outlined
6371+
// function
6372+
EXPECT_EQ(OutlinedFn->getFnAttribute("target-cpu"),
6373+
F->getFnAttribute("target-cpu"));
6374+
EXPECT_EQ(OutlinedFn->getFnAttribute("target-features"),
6375+
F->getFnAttribute("target-features"));
6376+
63596377
EXPECT_TRUE(OutlinedFn->hasWeakODRLinkage());
63606378
// Account for the "implicit" first argument.
63616379
EXPECT_EQ(OutlinedFn->getName(), "__omp_offloading_1_2_parent_l3");
@@ -6602,6 +6620,13 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDeviceSPMD) {
66026620
EXPECT_NE(OutlinedFn, nullptr);
66036621
EXPECT_NE(F, OutlinedFn);
66046622

6623+
// Check that target-cpu and target-features were propagated to the outlined
6624+
// function
6625+
EXPECT_EQ(OutlinedFn->getFnAttribute("target-cpu"),
6626+
F->getFnAttribute("target-cpu"));
6627+
EXPECT_EQ(OutlinedFn->getFnAttribute("target-features"),
6628+
F->getFnAttribute("target-features"));
6629+
66056630
EXPECT_TRUE(OutlinedFn->hasWeakODRLinkage());
66066631
// Account for the "implicit" first argument.
66076632
EXPECT_EQ(OutlinedFn->getName(), "__omp_offloading_1_2_parent_l3");

0 commit comments

Comments
 (0)