Skip to content

Commit b350937

Browse files
anchurajaaryanshukla
authored andcommitted
[OpenMP]Support for lowering masked op (llvm#98401)
1 parent fb66c47 commit b350937

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,43 @@ static llvm::omp::ProcBindKind getProcBindKind(omp::ClauseProcBindKind kind) {
264264
llvm_unreachable("Unknown ClauseProcBindKind kind");
265265
}
266266

267+
/// Converts an OpenMP 'masked' operation into LLVM IR using OpenMPIRBuilder.
268+
static LogicalResult
269+
convertOmpMasked(Operation &opInst, llvm::IRBuilderBase &builder,
270+
LLVM::ModuleTranslation &moduleTranslation) {
271+
auto maskedOp = cast<omp::MaskedOp>(opInst);
272+
using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
273+
// TODO: support error propagation in OpenMPIRBuilder and use it instead of
274+
// relying on captured variables.
275+
LogicalResult bodyGenStatus = success();
276+
277+
auto bodyGenCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP) {
278+
// MaskedOp has only one region associated with it.
279+
auto &region = maskedOp.getRegion();
280+
builder.restoreIP(codeGenIP);
281+
convertOmpOpRegions(region, "omp.masked.region", builder, moduleTranslation,
282+
bodyGenStatus);
283+
};
284+
285+
// TODO: Perform finalization actions for variables. This has to be
286+
// called for variables which have destructors/finalizers.
287+
auto finiCB = [&](InsertPointTy codeGenIP) {};
288+
289+
llvm::Value *filterVal = nullptr;
290+
if (auto filterVar = maskedOp.getFilteredThreadId()) {
291+
filterVal = moduleTranslation.lookupValue(filterVar);
292+
} else {
293+
llvm::LLVMContext &llvmContext = builder.getContext();
294+
filterVal =
295+
llvm::ConstantInt::get(llvm::Type::getInt32Ty(llvmContext), /*V=*/0);
296+
}
297+
assert(filterVal != nullptr);
298+
llvm::OpenMPIRBuilder::LocationDescription ompLoc(builder);
299+
builder.restoreIP(moduleTranslation.getOpenMPBuilder()->createMasked(
300+
ompLoc, bodyGenCB, finiCB, filterVal));
301+
return success();
302+
}
303+
267304
/// Converts an OpenMP 'master' operation into LLVM IR using OpenMPIRBuilder.
268305
static LogicalResult
269306
convertOmpMaster(Operation &opInst, llvm::IRBuilderBase &builder,
@@ -3414,6 +3451,9 @@ convertHostOrTargetOperation(Operation *op, llvm::IRBuilderBase &builder,
34143451
.Case([&](omp::ParallelOp op) {
34153452
return convertOmpParallel(op, builder, moduleTranslation);
34163453
})
3454+
.Case([&](omp::MaskedOp) {
3455+
return convertOmpMasked(*op, builder, moduleTranslation);
3456+
})
34173457
.Case([&](omp::MasterOp) {
34183458
return convertOmpMaster(*op, builder, moduleTranslation);
34193459
})

mlir/test/Target/LLVMIR/openmp-llvm.mlir

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,26 @@ llvm.func @test_omp_master() -> () {
310310

311311
// -----
312312

313+
// CHECK-LABEL: define void @test_omp_masked({{.*}})
314+
llvm.func @test_omp_masked(%arg0: i32)-> () {
315+
// CHECK: call void {{.*}}@__kmpc_fork_call{{.*}} @{{.*}})
316+
// CHECK: omp.par.region1:
317+
omp.parallel {
318+
omp.masked filter(%arg0: i32) {
319+
// CHECK: [[OMP_THREAD_3_4:%.*]] = call i32 @__kmpc_global_thread_num(ptr @{{[0-9]+}})
320+
// CHECK: {{[0-9]+}} = call i32 @__kmpc_masked(ptr @{{[0-9]+}}, i32 [[OMP_THREAD_3_4]], i32 %{{[0-9]+}})
321+
// CHECK: omp.masked.region
322+
// CHECK: call void @__kmpc_end_masked(ptr @{{[0-9]+}}, i32 [[OMP_THREAD_3_4]])
323+
// CHECK: br label %omp_region.end
324+
omp.terminator
325+
}
326+
omp.terminator
327+
}
328+
llvm.return
329+
}
330+
331+
// -----
332+
313333
// CHECK: %struct.ident_t = type
314334
// CHECK: @[[$loc:.*]] = private unnamed_addr constant {{.*}} c";unknown;unknown;{{[0-9]+}};{{[0-9]+}};;\00"
315335
// CHECK: @[[$loc_struct:.*]] = private unnamed_addr constant %struct.ident_t {{.*}} @[[$loc]] {{.*}}

0 commit comments

Comments
 (0)