Skip to content

Commit e2b3ac4

Browse files
committed
Address review comments
1 parent f120456 commit e2b3ac4

File tree

3 files changed

+68
-92
lines changed

3 files changed

+68
-92
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,9 +1387,6 @@ class OpenMPIRBuilder {
13871387

13881388
/// Supporting functions for Reductions CodeGen.
13891389
private:
1390-
/// Emit the llvm.used metadata.
1391-
void emitUsed(StringRef Name, std::vector<llvm::WeakTrackingVH> &List);
1392-
13931390
/// Get the id of the current thread on the GPU.
13941391
Value *getGPUThreadID();
13951392

@@ -2011,6 +2008,13 @@ class OpenMPIRBuilder {
20112008
/// Value.
20122009
GlobalValue *createGlobalFlag(unsigned Value, StringRef Name);
20132010

2011+
/// Emit the llvm.used metadata.
2012+
void emitUsed(StringRef Name, ArrayRef<llvm::WeakTrackingVH> List);
2013+
2014+
/// Emit the kernel execution mode.
2015+
GlobalVariable *emitKernelExecutionMode(StringRef KernelName,
2016+
omp::OMPTgtExecModeFlags Mode);
2017+
20142018
/// Generate control flow and cleanup for cancellation.
20152019
///
20162020
/// \param CancelFlag Flag indicating if the cancellation is performed.

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 37 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,38 @@ GlobalValue *OpenMPIRBuilder::createGlobalFlag(unsigned Value, StringRef Name) {
831831
return GV;
832832
}
833833

834+
void OpenMPIRBuilder::emitUsed(StringRef Name, ArrayRef<WeakTrackingVH> List) {
835+
if (List.empty())
836+
return;
837+
838+
// Convert List to what ConstantArray needs.
839+
SmallVector<Constant *, 8> UsedArray;
840+
UsedArray.resize(List.size());
841+
for (unsigned I = 0, E = List.size(); I != E; ++I)
842+
UsedArray[I] = ConstantExpr::getPointerBitCastOrAddrSpaceCast(
843+
cast<Constant>(&*List[I]), Builder.getPtrTy());
844+
845+
if (UsedArray.empty())
846+
return;
847+
ArrayType *ATy = ArrayType::get(Builder.getPtrTy(), UsedArray.size());
848+
849+
auto *GV = new GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
850+
ConstantArray::get(ATy, UsedArray), Name);
851+
852+
GV->setSection("llvm.metadata");
853+
}
854+
855+
GlobalVariable *
856+
OpenMPIRBuilder::emitKernelExecutionMode(StringRef KernelName,
857+
OMPTgtExecModeFlags Mode) {
858+
auto *Int8Ty = Builder.getInt8Ty();
859+
auto *GVMode = new GlobalVariable(
860+
M, Int8Ty, /*isConstant=*/true, GlobalValue::WeakAnyLinkage,
861+
ConstantInt::get(Int8Ty, Mode), Twine(KernelName, "_exec_mode"));
862+
GVMode->setVisibility(GlobalVariable::ProtectedVisibility);
863+
return GVMode;
864+
}
865+
834866
Constant *OpenMPIRBuilder::getOrCreateIdent(Constant *SrcLocStr,
835867
uint32_t SrcLocStrSize,
836868
IdentFlag LocFlags,
@@ -2242,28 +2274,6 @@ static OpenMPIRBuilder::InsertPointTy getInsertPointAfterInstr(Instruction *I) {
22422274
return OpenMPIRBuilder::InsertPointTy(I->getParent(), IT);
22432275
}
22442276

2245-
void OpenMPIRBuilder::emitUsed(StringRef Name,
2246-
std::vector<WeakTrackingVH> &List) {
2247-
if (List.empty())
2248-
return;
2249-
2250-
// Convert List to what ConstantArray needs.
2251-
SmallVector<Constant *, 8> UsedArray;
2252-
UsedArray.resize(List.size());
2253-
for (unsigned I = 0, E = List.size(); I != E; ++I)
2254-
UsedArray[I] = ConstantExpr::getPointerBitCastOrAddrSpaceCast(
2255-
cast<Constant>(&*List[I]), Builder.getPtrTy());
2256-
2257-
if (UsedArray.empty())
2258-
return;
2259-
ArrayType *ATy = ArrayType::get(Builder.getPtrTy(), UsedArray.size());
2260-
2261-
auto *GV = new GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
2262-
ConstantArray::get(ATy, UsedArray), Name);
2263-
2264-
GV->setSection("llvm.metadata");
2265-
}
2266-
22672277
Value *OpenMPIRBuilder::getGPUThreadID() {
22682278
return Builder.CreateCall(
22692279
getOrCreateRuntimeFunction(M,
@@ -6727,41 +6737,6 @@ FunctionCallee OpenMPIRBuilder::createDispatchDeinitFunction() {
67276737
return getOrCreateRuntimeFunction(M, omp::OMPRTL___kmpc_dispatch_deinit);
67286738
}
67296739

6730-
static void emitUsed(StringRef Name, std::vector<llvm::WeakTrackingVH> &List,
6731-
Module &M) {
6732-
if (List.empty())
6733-
return;
6734-
6735-
Type *PtrTy = PointerType::get(M.getContext(), /*AddressSpace=*/0);
6736-
6737-
// Convert List to what ConstantArray needs.
6738-
SmallVector<Constant *, 8> UsedArray;
6739-
UsedArray.reserve(List.size());
6740-
for (auto Item : List)
6741-
UsedArray.push_back(ConstantExpr::getPointerBitCastOrAddrSpaceCast(
6742-
cast<Constant>(&*Item), PtrTy));
6743-
6744-
ArrayType *ArrTy = ArrayType::get(PtrTy, UsedArray.size());
6745-
auto *GV =
6746-
new GlobalVariable(M, ArrTy, false, llvm::GlobalValue::AppendingLinkage,
6747-
llvm::ConstantArray::get(ArrTy, UsedArray), Name);
6748-
6749-
GV->setSection("llvm.metadata");
6750-
}
6751-
6752-
static void
6753-
emitExecutionMode(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
6754-
StringRef FunctionName, OMPTgtExecModeFlags Mode,
6755-
std::vector<llvm::WeakTrackingVH> &LLVMCompilerUsed) {
6756-
auto *Int8Ty = Type::getInt8Ty(Builder.getContext());
6757-
auto *GVMode = new llvm::GlobalVariable(
6758-
OMPBuilder.M, Int8Ty, /*isConstant=*/true,
6759-
llvm::GlobalValue::WeakAnyLinkage, llvm::ConstantInt::get(Int8Ty, Mode),
6760-
Twine(FunctionName, "_exec_mode"));
6761-
GVMode->setVisibility(llvm::GlobalVariable::ProtectedVisibility);
6762-
LLVMCompilerUsed.emplace_back(GVMode);
6763-
}
6764-
67656740
static Expected<Function *> createOutlinedFunction(
67666741
OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder, bool IsSPMD,
67676742
const OpenMPIRBuilder::TargetKernelDefaultAttrs &DefaultAttrs,
@@ -6794,12 +6769,9 @@ static Expected<Function *> createOutlinedFunction(
67946769
Function::Create(FuncType, GlobalValue::InternalLinkage, FuncName, M);
67956770

67966771
if (OMPBuilder.Config.isTargetDevice()) {
6797-
std::vector<llvm::WeakTrackingVH> LLVMCompilerUsed;
6798-
emitExecutionMode(OMPBuilder, Builder, FuncName,
6799-
IsSPMD ? OMP_TGT_EXEC_MODE_SPMD
6800-
: OMP_TGT_EXEC_MODE_GENERIC,
6801-
LLVMCompilerUsed);
6802-
emitUsed("llvm.compiler.used", LLVMCompilerUsed, OMPBuilder.M);
6772+
Value *ExecMode = OMPBuilder.emitKernelExecutionMode(
6773+
FuncName, IsSPMD ? OMP_TGT_EXEC_MODE_SPMD : OMP_TGT_EXEC_MODE_GENERIC);
6774+
OMPBuilder.emitUsed("llvm.compiler.used", {ExecMode});
68036775
}
68046776

68056777
// Save insert point.
@@ -7457,8 +7429,8 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
74577429
? InitMaxThreadsClause(RuntimeAttrs.MaxThreads)
74587430
: nullptr;
74597431

7460-
for (auto [TeamsVal, TargetVal] : llvm::zip_equal(
7461-
RuntimeAttrs.TeamsThreadLimit, RuntimeAttrs.TargetThreadLimit)) {
7432+
for (auto [TeamsVal, TargetVal] : zip_equal(RuntimeAttrs.TeamsThreadLimit,
7433+
RuntimeAttrs.TargetThreadLimit)) {
74627434
Value *TeamsThreadLimitClause = InitMaxThreadsClause(TeamsVal);
74637435
Value *NumThreads = InitMaxThreadsClause(TargetVal);
74647436

@@ -7518,8 +7490,6 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createTarget(
75187490
OpenMPIRBuilder::TargetBodyGenCallbackTy CBFunc,
75197491
OpenMPIRBuilder::TargetGenArgAccessorsCallbackTy ArgAccessorFuncCB,
75207492
SmallVector<DependData> Dependencies, bool HasNowait) {
7521-
assert((!RuntimeAttrs.LoopTripCount || IsSPMD) &&
7522-
"trip count not expected if IsSPMD=false");
75237493

75247494
if (!updateToLocation(Loc))
75257495
return InsertPointTy();

llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6459,18 +6459,19 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionSPMD) {
64596459
return Builder.saveIP();
64606460
};
64616461

6462-
auto SimpleArgAccessorCB =
6463-
[&](llvm::Argument &, llvm::Value *, llvm::Value *&,
6464-
llvm::OpenMPIRBuilder::InsertPointTy,
6465-
llvm::OpenMPIRBuilder::InsertPointTy CodeGenIP) {
6466-
Builder.restoreIP(CodeGenIP);
6467-
return Builder.saveIP();
6468-
};
6462+
auto SimpleArgAccessorCB = [&](Argument &, Value *, Value *&,
6463+
OpenMPIRBuilder::InsertPointTy,
6464+
OpenMPIRBuilder::InsertPointTy CodeGenIP) {
6465+
Builder.restoreIP(CodeGenIP);
6466+
return Builder.saveIP();
6467+
};
64696468

6470-
llvm::SmallVector<llvm::Value *> Inputs;
6471-
llvm::OpenMPIRBuilder::MapInfosTy CombinedInfos;
6472-
auto GenMapInfoCB = [&](llvm::OpenMPIRBuilder::InsertPointTy)
6473-
-> llvm::OpenMPIRBuilder::MapInfosTy & { return CombinedInfos; };
6469+
SmallVector<Value *> Inputs;
6470+
OpenMPIRBuilder::MapInfosTy CombinedInfos;
6471+
auto GenMapInfoCB =
6472+
[&](OpenMPIRBuilder::InsertPointTy) -> OpenMPIRBuilder::MapInfosTy & {
6473+
return CombinedInfos;
6474+
};
64746475

64756476
TargetRegionEntryInfo EntryInfo("func", 42, 4711, 17);
64766477
OpenMPIRBuilder::LocationDescription OmpLoc({Builder.saveIP(), DL});
@@ -6547,19 +6548,20 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDeviceSPMD) {
65476548
OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
65486549

65496550
Function *OutlinedFn = nullptr;
6550-
llvm::SmallVector<llvm::Value *> CapturedArgs;
6551+
SmallVector<Value *> CapturedArgs;
65516552

6552-
auto SimpleArgAccessorCB =
6553-
[&](llvm::Argument &, llvm::Value *, llvm::Value *&,
6554-
llvm::OpenMPIRBuilder::InsertPointTy,
6555-
llvm::OpenMPIRBuilder::InsertPointTy CodeGenIP) {
6556-
Builder.restoreIP(CodeGenIP);
6557-
return Builder.saveIP();
6558-
};
6553+
auto SimpleArgAccessorCB = [&](Argument &, Value *, Value *&,
6554+
OpenMPIRBuilder::InsertPointTy,
6555+
OpenMPIRBuilder::InsertPointTy CodeGenIP) {
6556+
Builder.restoreIP(CodeGenIP);
6557+
return Builder.saveIP();
6558+
};
65596559

6560-
llvm::OpenMPIRBuilder::MapInfosTy CombinedInfos;
6561-
auto GenMapInfoCB = [&](llvm::OpenMPIRBuilder::InsertPointTy)
6562-
-> llvm::OpenMPIRBuilder::MapInfosTy & { return CombinedInfos; };
6560+
OpenMPIRBuilder::MapInfosTy CombinedInfos;
6561+
auto GenMapInfoCB =
6562+
[&](OpenMPIRBuilder::InsertPointTy) -> OpenMPIRBuilder::MapInfosTy & {
6563+
return CombinedInfos;
6564+
};
65636565

65646566
auto BodyGenCB = [&](OpenMPIRBuilder::InsertPointTy,
65656567
OpenMPIRBuilder::InsertPointTy CodeGenIP)

0 commit comments

Comments
 (0)