Skip to content

Commit fe7ca1a

Browse files
committed
[mlir][openacc] Initial translation for DataOp to LLVM IR
Add basic translation of acc.data to LLVM IR with runtime calls. Reviewed By: jdoerfert Differential Revision: https://reviews.llvm.org/D104301
1 parent 3c45476 commit fe7ca1a

File tree

5 files changed

+458
-66
lines changed

5 files changed

+458
-66
lines changed

llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,31 @@ class OpenMPIRBuilder {
636636
createOffloadMapnames(SmallVectorImpl<llvm::Constant *> &Names,
637637
std::string VarName);
638638

639+
struct MapperAllocas {
640+
AllocaInst *ArgsBase = nullptr;
641+
AllocaInst *Args = nullptr;
642+
AllocaInst *ArgSizes = nullptr;
643+
};
644+
645+
/// Create the allocas instruction used in call to mapper functions.
646+
void createMapperAllocas(const LocationDescription &Loc,
647+
InsertPointTy AllocaIP, unsigned NumOperands,
648+
struct MapperAllocas &MapperAllocas);
649+
650+
/// Create the call for the target mapper function.
651+
/// \param Loc The source location description.
652+
/// \param MapperFunc Function to be called.
653+
/// \param SrcLocInfo Source location information global.
654+
/// \param MaptypesArgs
655+
/// \param MapnamesArg
656+
/// \param MapperAllocas The AllocaInst used for the call.
657+
/// \param DeviceID Device ID for the call.
658+
/// \param TotalNbOperand Number of operand in the call.
659+
void emitMapperCall(const LocationDescription &Loc, Function *MapperFunc,
660+
Value *SrcLocInfo, Value *MaptypesArg, Value *MapnamesArg,
661+
struct MapperAllocas &MapperAllocas, int64_t DeviceID,
662+
unsigned NumOperands);
663+
639664
public:
640665
/// Generator for __kmpc_copyprivate
641666
///

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,6 +2318,51 @@ OpenMPIRBuilder::createOffloadMaptypes(SmallVectorImpl<uint64_t> &Mappings,
23182318
return MaptypesArrayGlobal;
23192319
}
23202320

2321+
void OpenMPIRBuilder::createMapperAllocas(const LocationDescription &Loc,
2322+
InsertPointTy AllocaIP,
2323+
unsigned NumOperands,
2324+
struct MapperAllocas &MapperAllocas) {
2325+
if (!updateToLocation(Loc))
2326+
return;
2327+
2328+
auto *ArrI8PtrTy = ArrayType::get(Int8Ptr, NumOperands);
2329+
auto *ArrI64Ty = ArrayType::get(Int64, NumOperands);
2330+
Builder.restoreIP(AllocaIP);
2331+
AllocaInst *ArgsBase = Builder.CreateAlloca(ArrI8PtrTy);
2332+
AllocaInst *Args = Builder.CreateAlloca(ArrI8PtrTy);
2333+
AllocaInst *ArgSizes = Builder.CreateAlloca(ArrI64Ty);
2334+
Builder.restoreIP(Loc.IP);
2335+
MapperAllocas.ArgsBase = ArgsBase;
2336+
MapperAllocas.Args = Args;
2337+
MapperAllocas.ArgSizes = ArgSizes;
2338+
}
2339+
2340+
void OpenMPIRBuilder::emitMapperCall(const LocationDescription &Loc,
2341+
Function *MapperFunc, Value *SrcLocInfo,
2342+
Value *MaptypesArg, Value *MapnamesArg,
2343+
struct MapperAllocas &MapperAllocas,
2344+
int64_t DeviceID, unsigned NumOperands) {
2345+
if (!updateToLocation(Loc))
2346+
return;
2347+
2348+
auto *ArrI8PtrTy = ArrayType::get(Int8Ptr, NumOperands);
2349+
auto *ArrI64Ty = ArrayType::get(Int64, NumOperands);
2350+
Value *ArgsBaseGEP =
2351+
Builder.CreateInBoundsGEP(ArrI8PtrTy, MapperAllocas.ArgsBase,
2352+
{Builder.getInt32(0), Builder.getInt32(0)});
2353+
Value *ArgsGEP =
2354+
Builder.CreateInBoundsGEP(ArrI8PtrTy, MapperAllocas.Args,
2355+
{Builder.getInt32(0), Builder.getInt32(0)});
2356+
Value *ArgSizesGEP =
2357+
Builder.CreateInBoundsGEP(ArrI64Ty, MapperAllocas.ArgSizes,
2358+
{Builder.getInt32(0), Builder.getInt32(0)});
2359+
Value *NullPtr = Constant::getNullValue(Int8Ptr->getPointerTo());
2360+
Builder.CreateCall(MapperFunc,
2361+
{SrcLocInfo, Builder.getInt64(DeviceID),
2362+
Builder.getInt32(NumOperands), ArgsBaseGEP, ArgsGEP,
2363+
ArgSizesGEP, MaptypesArg, MapnamesArg, NullPtr});
2364+
}
2365+
23212366
bool OpenMPIRBuilder::checkAndEmitFlushAfterAtomic(
23222367
const LocationDescription &Loc, llvm::AtomicOrdering AO, AtomicKind AK) {
23232368
assert(!(AO == AtomicOrdering::NotAtomic ||

llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2703,4 +2703,110 @@ TEST_F(OpenMPIRBuilderTest, CreateOffloadMapnames) {
27032703
EXPECT_EQ(Initializer->getType()->getArrayNumElements(), Names.size());
27042704
}
27052705

2706+
TEST_F(OpenMPIRBuilderTest, CreateMapperAllocas) {
2707+
OpenMPIRBuilder OMPBuilder(*M);
2708+
OMPBuilder.initialize();
2709+
F->setName("func");
2710+
IRBuilder<> Builder(BB);
2711+
2712+
OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
2713+
2714+
unsigned TotalNbOperand = 2;
2715+
2716+
OpenMPIRBuilder::MapperAllocas MapperAllocas;
2717+
IRBuilder<>::InsertPoint AllocaIP(&F->getEntryBlock(),
2718+
F->getEntryBlock().getFirstInsertionPt());
2719+
OMPBuilder.createMapperAllocas(Loc, AllocaIP, TotalNbOperand, MapperAllocas);
2720+
EXPECT_NE(MapperAllocas.ArgsBase, nullptr);
2721+
EXPECT_NE(MapperAllocas.Args, nullptr);
2722+
EXPECT_NE(MapperAllocas.ArgSizes, nullptr);
2723+
EXPECT_TRUE(MapperAllocas.ArgsBase->getAllocatedType()->isArrayTy());
2724+
ArrayType *ArrType =
2725+
dyn_cast<ArrayType>(MapperAllocas.ArgsBase->getAllocatedType());
2726+
EXPECT_EQ(ArrType->getNumElements(), TotalNbOperand);
2727+
EXPECT_TRUE(MapperAllocas.ArgsBase->getAllocatedType()
2728+
->getArrayElementType()
2729+
->isPointerTy());
2730+
EXPECT_TRUE(MapperAllocas.ArgsBase->getAllocatedType()
2731+
->getArrayElementType()
2732+
->getPointerElementType()
2733+
->isIntegerTy(8));
2734+
2735+
EXPECT_TRUE(MapperAllocas.Args->getAllocatedType()->isArrayTy());
2736+
ArrType = dyn_cast<ArrayType>(MapperAllocas.Args->getAllocatedType());
2737+
EXPECT_EQ(ArrType->getNumElements(), TotalNbOperand);
2738+
EXPECT_TRUE(MapperAllocas.Args->getAllocatedType()
2739+
->getArrayElementType()
2740+
->isPointerTy());
2741+
EXPECT_TRUE(MapperAllocas.Args->getAllocatedType()
2742+
->getArrayElementType()
2743+
->getPointerElementType()
2744+
->isIntegerTy(8));
2745+
2746+
EXPECT_TRUE(MapperAllocas.ArgSizes->getAllocatedType()->isArrayTy());
2747+
ArrType = dyn_cast<ArrayType>(MapperAllocas.ArgSizes->getAllocatedType());
2748+
EXPECT_EQ(ArrType->getNumElements(), TotalNbOperand);
2749+
EXPECT_TRUE(MapperAllocas.ArgSizes->getAllocatedType()
2750+
->getArrayElementType()
2751+
->isIntegerTy(64));
2752+
}
2753+
2754+
TEST_F(OpenMPIRBuilderTest, EmitMapperCall) {
2755+
OpenMPIRBuilder OMPBuilder(*M);
2756+
OMPBuilder.initialize();
2757+
F->setName("func");
2758+
IRBuilder<> Builder(BB);
2759+
LLVMContext &Ctx = M->getContext();
2760+
2761+
OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
2762+
2763+
unsigned TotalNbOperand = 2;
2764+
2765+
OpenMPIRBuilder::MapperAllocas MapperAllocas;
2766+
IRBuilder<>::InsertPoint AllocaIP(&F->getEntryBlock(),
2767+
F->getEntryBlock().getFirstInsertionPt());
2768+
OMPBuilder.createMapperAllocas(Loc, AllocaIP, TotalNbOperand, MapperAllocas);
2769+
2770+
auto *BeginMapperFunc = OMPBuilder.getOrCreateRuntimeFunctionPtr(
2771+
omp::OMPRTL___tgt_target_data_begin_mapper);
2772+
2773+
SmallVector<uint64_t> Flags = {0, 2};
2774+
2775+
Constant *SrcLocCst = OMPBuilder.getOrCreateSrcLocStr("", "file1", 2, 5);
2776+
Value *SrcLocInfo = OMPBuilder.getOrCreateIdent(SrcLocCst);
2777+
2778+
Constant *Cst1 = OMPBuilder.getOrCreateSrcLocStr("array1", "file1", 2, 5);
2779+
Constant *Cst2 = OMPBuilder.getOrCreateSrcLocStr("array2", "file1", 3, 5);
2780+
SmallVector<llvm::Constant *> Names = {Cst1, Cst2};
2781+
2782+
GlobalVariable *Maptypes =
2783+
OMPBuilder.createOffloadMaptypes(Flags, ".offload_maptypes");
2784+
Value *MaptypesArg = Builder.CreateConstInBoundsGEP2_32(
2785+
ArrayType::get(Type::getInt64Ty(Ctx), TotalNbOperand), Maptypes,
2786+
/*Idx0=*/0, /*Idx1=*/0);
2787+
2788+
GlobalVariable *Mapnames =
2789+
OMPBuilder.createOffloadMapnames(Names, ".offload_mapnames");
2790+
Value *MapnamesArg = Builder.CreateConstInBoundsGEP2_32(
2791+
ArrayType::get(Type::getInt8PtrTy(Ctx), TotalNbOperand), Mapnames,
2792+
/*Idx0=*/0, /*Idx1=*/0);
2793+
2794+
OMPBuilder.emitMapperCall(Builder.saveIP(), BeginMapperFunc, SrcLocInfo,
2795+
MaptypesArg, MapnamesArg, MapperAllocas, -1,
2796+
TotalNbOperand);
2797+
2798+
CallInst *MapperCall = dyn_cast<CallInst>(&BB->back());
2799+
EXPECT_NE(MapperCall, nullptr);
2800+
EXPECT_EQ(MapperCall->getNumArgOperands(), 9U);
2801+
EXPECT_EQ(MapperCall->getCalledFunction()->getName(),
2802+
"__tgt_target_data_begin_mapper");
2803+
EXPECT_EQ(MapperCall->getOperand(0), SrcLocInfo);
2804+
EXPECT_TRUE(MapperCall->getOperand(1)->getType()->isIntegerTy(64));
2805+
EXPECT_TRUE(MapperCall->getOperand(2)->getType()->isIntegerTy(32));
2806+
2807+
EXPECT_EQ(MapperCall->getOperand(6), MaptypesArg);
2808+
EXPECT_EQ(MapperCall->getOperand(7), MapnamesArg);
2809+
EXPECT_TRUE(MapperCall->getOperand(8)->getType()->isPointerTy());
2810+
}
2811+
27062812
} // namespace

0 commit comments

Comments
 (0)