diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPKinds.def b/llvm/include/llvm/Frontend/OpenMP/OMPKinds.def --- a/llvm/include/llvm/Frontend/OpenMP/OMPKinds.def +++ b/llvm/include/llvm/Frontend/OpenMP/OMPKinds.def @@ -499,6 +499,11 @@ EnumAttr(WillReturn), EnumAttr(NoFree)) : AttributeSet(EnumAttr(NoUnwind))) +__OMP_ATTRS_SET(NoCaptureAttrs, + OptimisticAttributes + ? AttributeSet(EnumAttr(NoCapture)) + : AttributeSet(EnumAttr(NoCapture))) + #if 0 __OMP_ATTRS_SET(InaccessibleOnlyAttrs, OptimisticAttributes @@ -840,6 +845,11 @@ __OMP_RTL_ATTRS(__kmpc_doacross_fini, BarrierAttrs, AttributeSet(), ParamAttrs(ReadOnlyPtrAttrs)) +__OMP_RTL_ATTRS(__kmpc_alloc_shared, DefaultAttrs, ReturnPtrAttrs, + ParamAttrs()) +__OMP_RTL_ATTRS(__kmpc_free_shared, AllocAttrs, AttributeSet(), + ParamAttrs(NoCaptureAttrs)) + __OMP_RTL_ATTRS(__kmpc_alloc, DefaultAttrs, ReturnPtrAttrs, ParamAttrs()) __OMP_RTL_ATTRS(__kmpc_free, AllocAttrs, AttributeSet(), ParamAttrs()) diff --git a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp --- a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp +++ b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp @@ -539,6 +539,8 @@ if (IsModulePass) { Changed |= runAttributor(); + OMPInfoCache.recollectUses(); + if (remarksEnabled()) analysisGlobalization(); } else { @@ -1149,28 +1151,23 @@ } void analysisGlobalization() { - RuntimeFunction GlobalizationRuntimeIDs[] = {OMPRTL___kmpc_alloc_shared, - OMPRTL___kmpc_free_shared}; - - for (const auto GlobalizationCallID : GlobalizationRuntimeIDs) { - auto &RFI = OMPInfoCache.RFIs[GlobalizationCallID]; - - auto CheckGlobalization = [&](Use &U, Function &Decl) { - if (CallInst *CI = getCallIfRegularCall(U, &RFI)) { - auto Remark = [&](OptimizationRemarkAnalysis ORA) { - return ORA - << "Found thread data sharing on the GPU. " - << "Expect degraded performance due to data globalization."; - }; - emitRemark(CI, "OpenMPGlobalization", - Remark); - } + auto &RFI = OMPInfoCache.RFIs[OMPRTL___kmpc_alloc_shared]; + + auto CheckGlobalization = [&](Use &U, Function &Decl) { + if (CallInst *CI = getCallIfRegularCall(U, &RFI)) { + auto Remark = [&](OptimizationRemarkAnalysis ORA) { + return ORA + << "Found thread data sharing on the GPU. " + << "Expect degraded performance due to data globalization."; + }; + emitRemark(CI, "OpenMPGlobalization", + Remark); + } - return false; - }; + return false; + }; - RFI.foreachUse(SCC, CheckGlobalization); - } + RFI.foreachUse(SCC, CheckGlobalization); } /// Maps the values stored in the offload arrays passed as arguments to @@ -1639,6 +1636,21 @@ GetterRFI.foreachUse(SCC, CreateAA); } + // Create HeapToStack AA for all globalization calls + RuntimeFunction GlobalizationCallID = OMPRTL___kmpc_alloc_shared; + auto &GlobalizationRFI = OMPInfoCache.RFIs[GlobalizationCallID]; + auto CreateAA = [&](Use &U, Function &Decl) { + CallInst *CI = OpenMPOpt::getCallIfRegularCall(U, &GlobalizationRFI); + if (!CI) + return false; + + IRPosition FnPos = IRPosition::function(*CI->getFunction()); + A.getOrCreateAAFor(FnPos); + return false; + }; + GlobalizationRFI.foreachUse(SCC, CreateAA); + + // Create an ExecutionDomain AA for every function for (auto &F : M) { if (!F.isDeclaration()) A.getOrCreateAAFor(IRPosition::function(F)); diff --git a/llvm/test/Transforms/OpenMP/remove_globalization.ll b/llvm/test/Transforms/OpenMP/remove_globalization.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/OpenMP/remove_globalization.ll @@ -0,0 +1,46 @@ +; RUN: opt -S -passes=openmp-opt < %s | FileCheck %s +target datalayout = "e-i64:64-i128:128-v16:16-v32:32-n16:32:64" +target triple = "nvptx64" + +@S = external local_unnamed_addr global i8* + +; CHECK-NOT: {{.*}} = call i8* @__kmpc_alloc_shared({{.*}}) +; CHECK-NOT: call void @__kmpc_free_shared({{.*}}) +define void @foo() #0 { +entry: + %0 = call i8* @__kmpc_alloc_shared(i64 4) + %x_on_stack = bitcast i8* %0 to i32* + %1 = bitcast i32* %x_on_stack to i8* + call void @use(i8* %1) + call void @__kmpc_free_shared(i8* %0) + ret void +} + +; CHECK: {{.*}} = call i8* @__kmpc_alloc_shared({{.*}}) +; CHECK: call void @__kmpc_free_shared({{.*}}) +define void @bar() #0 { +entry: + %0 = call i8* @__kmpc_alloc_shared(i64 4) + %x_on_stack = bitcast i8* %0 to i32* + %1 = bitcast i32* %x_on_stack to i8* + call void @share(i8* %1) + call void @__kmpc_free_shared(i8* %0) + ret void +} + +define void @use(i8* %x) { +entry: + ret void +} + +define void @share(i8* %x) { +entry: + store i8* %x, i8** @S + ret void +} + +; CHECK: declare i8* @__kmpc_alloc_shared(i64) +declare i8* @__kmpc_alloc_shared(i64) + +; CHECK: declare void @__kmpc_free_shared(i8* nocapture) +declare void @__kmpc_free_shared(i8*) diff --git a/llvm/test/Transforms/PhaseOrdering/openmp-opt-module.ll b/llvm/test/Transforms/PhaseOrdering/openmp-opt-module.ll --- a/llvm/test/Transforms/PhaseOrdering/openmp-opt-module.ll +++ b/llvm/test/Transforms/PhaseOrdering/openmp-opt-module.ll @@ -3,6 +3,8 @@ @.str = private unnamed_addr constant [13 x i8] c"Alloc Shared\00", align 1 +@S = external local_unnamed_addr global i8* + ; MODULE: remark: openmp_opt_module.c:5:7: Found thread data sharing on the GPU. Expect degraded performance due to data globalization. define void @foo() { @@ -17,12 +19,11 @@ define void @use(i8* %0) { entry: - %.addr = alloca i8*, align 8 - store i8* %0, i8** %.addr, align 8 + store i8* %0, i8** @S ret void } -define internal i8* @__kmpc_alloc_shared(i64 %DataSize) { +define weak i8* @__kmpc_alloc_shared(i64 %DataSize) { entry: %call = call i8* @_Z10SafeMallocmPKc(i64 %DataSize, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0)) #11 ret i8* %call