Skip to content

Commit e9ea3a5

Browse files
committed
Address review comments
1 parent 2fbe762 commit e9ea3a5

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
@@ -830,6 +830,38 @@ GlobalValue *OpenMPIRBuilder::createGlobalFlag(unsigned Value, StringRef Name) {
830830
return GV;
831831
}
832832

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

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

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

68006775
if (OMPBuilder.Config.isTargetDevice()) {
6801-
std::vector<llvm::WeakTrackingVH> LLVMCompilerUsed;
6802-
emitExecutionMode(OMPBuilder, Builder, FuncName,
6803-
IsSPMD ? OMP_TGT_EXEC_MODE_SPMD
6804-
: OMP_TGT_EXEC_MODE_GENERIC,
6805-
LLVMCompilerUsed);
6806-
emitUsed("llvm.compiler.used", LLVMCompilerUsed, OMPBuilder.M);
6776+
Value *ExecMode = OMPBuilder.emitKernelExecutionMode(
6777+
FuncName, IsSPMD ? OMP_TGT_EXEC_MODE_SPMD : OMP_TGT_EXEC_MODE_GENERIC);
6778+
OMPBuilder.emitUsed("llvm.compiler.used", {ExecMode});
68076779
}
68086780

68096781
// Save insert point.
@@ -7460,8 +7432,8 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
74607432
? InitMaxThreadsClause(RuntimeAttrs.MaxThreads)
74617433
: nullptr;
74627434

7463-
for (auto [TeamsVal, TargetVal] : llvm::zip_equal(
7464-
RuntimeAttrs.TeamsThreadLimit, RuntimeAttrs.TargetThreadLimit)) {
7435+
for (auto [TeamsVal, TargetVal] : zip_equal(RuntimeAttrs.TeamsThreadLimit,
7436+
RuntimeAttrs.TargetThreadLimit)) {
74657437
Value *TeamsThreadLimitClause = InitMaxThreadsClause(TeamsVal);
74667438
Value *NumThreads = InitMaxThreadsClause(TargetVal);
74677439

@@ -7521,8 +7493,6 @@ OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createTarget(
75217493
OpenMPIRBuilder::TargetBodyGenCallbackTy CBFunc,
75227494
OpenMPIRBuilder::TargetGenArgAccessorsCallbackTy ArgAccessorFuncCB,
75237495
SmallVector<DependData> Dependencies, bool HasNowait) {
7524-
assert((!RuntimeAttrs.LoopTripCount || IsSPMD) &&
7525-
"trip count not expected if IsSPMD=false");
75267496

75277497
if (!updateToLocation(Loc))
75287498
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)