Skip to content

[OMPIRBuilder] Propagate attributes to outlined target regions #117875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6774,6 +6774,18 @@ static Expected<Function *> createOutlinedFunction(
auto Func =
Function::Create(FuncType, GlobalValue::InternalLinkage, FuncName, M);

// Forward target-cpu and target-features function attributes from the
// original function to the new outlined function.
Function *ParentFn = Builder.GetInsertBlock()->getParent();

auto TargetCpuAttr = ParentFn->getFnAttribute("target-cpu");
if (TargetCpuAttr.isStringAttribute())
Func->addFnAttr(TargetCpuAttr);

auto TargetFeaturesAttr = ParentFn->getFnAttribute("target-features");
if (TargetFeaturesAttr.isStringAttribute())
Func->addFnAttr(TargetFeaturesAttr);

if (OMPBuilder.Config.isTargetDevice()) {
Value *ExecMode =
OMPBuilder.emitKernelExecutionMode(FuncName, DefaultAttrs.ExecFlags);
Expand Down
25 changes: 25 additions & 0 deletions llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6169,6 +6169,8 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
OpenMPIRBuilderConfig Config(false, false, false, false, false, false, false);
OMPBuilder.setConfig(Config);
F->setName("func");
F->addFnAttr("target-cpu", "x86-64");
F->addFnAttr("target-features", "+mmx,+sse");
IRBuilder<> Builder(BB);
auto *Int32Ty = Builder.getInt32Ty();

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

// Check that target-cpu and target-features were propagated to the outlined
// function
EXPECT_EQ(OutlinedFunc->getFnAttribute("target-cpu"),
F->getFnAttribute("target-cpu"));
EXPECT_EQ(OutlinedFunc->getFnAttribute("target-features"),
F->getFnAttribute("target-features"));

EXPECT_FALSE(verifyModule(*M, &errs()));
}

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

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

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

// Check that target-cpu and target-features were propagated to the outlined
// function
EXPECT_EQ(OutlinedFn->getFnAttribute("target-cpu"),
F->getFnAttribute("target-cpu"));
EXPECT_EQ(OutlinedFn->getFnAttribute("target-features"),
F->getFnAttribute("target-features"));

EXPECT_TRUE(OutlinedFn->hasWeakODRLinkage());
// Account for the "implicit" first argument.
EXPECT_EQ(OutlinedFn->getName(), "__omp_offloading_1_2_parent_l3");
Expand Down Expand Up @@ -6657,6 +6675,13 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDeviceSPMD) {
EXPECT_NE(OutlinedFn, nullptr);
EXPECT_NE(F, OutlinedFn);

// Check that target-cpu and target-features were propagated to the outlined
// function
EXPECT_EQ(OutlinedFn->getFnAttribute("target-cpu"),
F->getFnAttribute("target-cpu"));
EXPECT_EQ(OutlinedFn->getFnAttribute("target-features"),
F->getFnAttribute("target-features"));

EXPECT_TRUE(OutlinedFn->hasWeakODRLinkage());
// Account for the "implicit" first argument.
EXPECT_EQ(OutlinedFn->getName(), "__omp_offloading_1_2_parent_l3");
Expand Down
Loading