Skip to content

Commit c7ca41f

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 e2b3ac4 commit c7ca41f

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
@@ -6768,6 +6768,18 @@ static Expected<Function *> createOutlinedFunction(
67686768
auto Func =
67696769
Function::Create(FuncType, GlobalValue::InternalLinkage, FuncName, M);
67706770

6771+
// Forward target-cpu and target-features function attributes from the
6772+
// original function to the new outlined function.
6773+
Function *ParentFn = Builder.GetInsertBlock()->getParent();
6774+
6775+
auto TargetCpuAttr = ParentFn->getFnAttribute("target-cpu");
6776+
if (TargetCpuAttr.isStringAttribute())
6777+
Func->addFnAttr(TargetCpuAttr);
6778+
6779+
auto TargetFeaturesAttr = ParentFn->getFnAttribute("target-features");
6780+
if (TargetFeaturesAttr.isStringAttribute())
6781+
Func->addFnAttr(TargetFeaturesAttr);
6782+
67716783
if (OMPBuilder.Config.isTargetDevice()) {
67726784
Value *ExecMode = OMPBuilder.emitKernelExecutionMode(
67736785
FuncName, IsSPMD ? OMP_TGT_EXEC_MODE_SPMD : OMP_TGT_EXEC_MODE_GENERIC);

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

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

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

@@ -6279,6 +6288,8 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
62796288
OMPBuilder.initialize();
62806289

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

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

6369+
// Check that target-cpu and target-features were propagated to the outlined
6370+
// function
6371+
EXPECT_EQ(OutlinedFn->getFnAttribute("target-cpu"),
6372+
F->getFnAttribute("target-cpu"));
6373+
EXPECT_EQ(OutlinedFn->getFnAttribute("target-features"),
6374+
F->getFnAttribute("target-features"));
6375+
63586376
EXPECT_TRUE(OutlinedFn->hasWeakODRLinkage());
63596377
// Account for the "implicit" first argument.
63606378
EXPECT_EQ(OutlinedFn->getName(), "__omp_offloading_1_2_parent_l3");
@@ -6594,6 +6612,13 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDeviceSPMD) {
65946612
EXPECT_NE(OutlinedFn, nullptr);
65956613
EXPECT_NE(F, OutlinedFn);
65966614

6615+
// Check that target-cpu and target-features were propagated to the outlined
6616+
// function
6617+
EXPECT_EQ(OutlinedFn->getFnAttribute("target-cpu"),
6618+
F->getFnAttribute("target-cpu"));
6619+
EXPECT_EQ(OutlinedFn->getFnAttribute("target-features"),
6620+
F->getFnAttribute("target-features"));
6621+
65976622
EXPECT_TRUE(OutlinedFn->hasWeakODRLinkage());
65986623
// Account for the "implicit" first argument.
65996624
EXPECT_EQ(OutlinedFn->getName(), "__omp_offloading_1_2_parent_l3");

0 commit comments

Comments
 (0)