Skip to content
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

[OpenMP][mlir] Add translation for if in omp.teams #69404

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ convertOmpTeams(omp::TeamsOp op, llvm::IRBuilderBase &builder,
LLVM::ModuleTranslation &moduleTranslation) {
using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
LogicalResult bodyGenStatus = success();
if (op.getIfExpr() || !op.getAllocatorsVars().empty() || op.getReductions())
if (!op.getAllocatorsVars().empty() || op.getReductions())
return op.emitError("unhandled clauses for translation to LLVM IR");

auto bodyCB = [&](InsertPointTy allocaIP, InsertPointTy codegenIP) {
Expand All @@ -689,9 +689,13 @@ convertOmpTeams(omp::TeamsOp op, llvm::IRBuilderBase &builder,
if (Value threadLimitVar = op.getThreadLimit())
threadLimit = moduleTranslation.lookupValue(threadLimitVar);

llvm::Value *ifExpr = nullptr;
if (Value ifExprVar = op.getIfExpr())
ifExpr = moduleTranslation.lookupValue(ifExprVar);

llvm::OpenMPIRBuilder::LocationDescription ompLoc(builder);
builder.restoreIP(moduleTranslation.getOpenMPBuilder()->createTeams(
ompLoc, bodyCB, numTeamsLower, numTeamsUpper, threadLimit));
ompLoc, bodyCB, numTeamsLower, numTeamsUpper, threadLimit, ifExpr));
return bodyGenStatus;
}

Expand Down
48 changes: 48 additions & 0 deletions mlir/test/Target/LLVMIR/openmp-teams.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,51 @@ llvm.func @omp_teams_num_teams_and_thread_limit(%numTeamsLower: i32, %numTeamsUp
// CHECK: define internal void [[OUTLINED_FN]](ptr {{.+}}, ptr {{.+}})
// CHECK: call void @duringTeams()
// CHECK: ret void

// -----

llvm.func @beforeTeams()
llvm.func @duringTeams()
llvm.func @afterTeams()

// CHECK-LABEL: @teams_if
// CHECK-SAME: (i1 [[ARG:.+]])
llvm.func @teams_if(%arg : i1) {
// CHECK-NEXT: call void @beforeTeams()
llvm.call @beforeTeams() : () -> ()
// CHECK: [[NUM_TEAMS_UPPER:%.+]] = select i1 [[ARG]], i32 0, i32 1
// CHECK: [[NUM_TEAMS_LOWER:%.+]] = select i1 [[ARG]], i32 0, i32 1
Comment on lines +253 to +254
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it 0 if ARG is true?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zero does not mean zero teams. The runtime call interprets zero as "not-specified". So, in that case, an "implementation-defined" number of teams are created.

When the condition is true, and there is no num_teams clause, the number of teams should be whatever they would be without the if clause - which in this case falls to the implementation defined number of teams. For LLVM OpenMP library, internally, it sets number of teams to one if zero is passed here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment in the test regarding this.

// CHECK: call void @__kmpc_push_num_teams_51(ptr {{.+}}, i32 {{.+}}, i32 [[NUM_TEAMS_LOWER]], i32 [[NUM_TEAMS_UPPER]], i32 0)
// CHECK: call void {{.+}} @__kmpc_fork_teams({{.+}})
omp.teams if(%arg) {
llvm.call @duringTeams() : () -> ()
omp.terminator
}
// CHECK: call void @afterTeams()
llvm.call @afterTeams() : () -> ()
llvm.return
}

// -----

llvm.func @beforeTeams()
llvm.func @duringTeams()
llvm.func @afterTeams()

// CHECK-LABEL: @teams_if_with_num_teams
// CHECK-SAME: (i1 [[CONDITION:.+]], i32 [[NUM_TEAMS_LOWER:.+]], i32 [[NUM_TEAMS_UPPER:.+]], i32 [[THREAD_LIMIT:.+]])
llvm.func @teams_if_with_num_teams(%condition: i1, %numTeamsLower: i32, %numTeamsUpper: i32, %threadLimit: i32) {
// CHECK: call void @beforeTeams()
llvm.call @beforeTeams() : () -> ()
// CHECK: [[NUM_TEAMS_UPPER_NEW:%.+]] = select i1 [[CONDITION]], i32 [[NUM_TEAMS_UPPER]], i32 1
// CHECK: [[NUM_TEAMS_LOWER_NEW:%.+]] = select i1 [[CONDITION]], i32 [[NUM_TEAMS_LOWER]], i32 1
// CHECK: call void @__kmpc_push_num_teams_51(ptr {{.+}}, i32 {{.+}}, i32 [[NUM_TEAMS_LOWER_NEW]], i32 [[NUM_TEAMS_UPPER_NEW]], i32 [[THREAD_LIMIT]])
// CHECK: call void {{.+}} @__kmpc_fork_teams({{.+}})
omp.teams if(%condition) num_teams(%numTeamsLower: i32 to %numTeamsUpper: i32) thread_limit(%threadLimit: i32) {
llvm.call @duringTeams() : () -> ()
omp.terminator
}
// CHECK: call void @afterTeams()
llvm.call @afterTeams() : () -> ()
llvm.return
}