From 472a2230725c6c4e8dfa1c059a75b60775a2df92 Mon Sep 17 00:00:00 2001 From: Joseph Huber Date: Wed, 21 Jul 2021 17:13:46 -0400 Subject: [PATCH] [OpenMP] Change `__kmpc_free_shared` to include the paired allocation size This patch changes `__kmpc_free_shared` to take an additional argument corresponding to the associated allocation's size. This makes it easier to implement the allocator in the runtime. Reviewed By: jdoerfert Differential Revision: https://reviews.llvm.org/D106496 --- include/llvm/Analysis/TargetLibraryInfo.def | 2 +- include/llvm/Frontend/OpenMP/OMPKinds.def | 2 +- lib/Analysis/MemoryBuiltins.cpp | 4 ++-- lib/Analysis/TargetLibraryInfo.cpp | 4 +++- test/Transforms/OpenMP/remove_globalization.ll | 13 +++++++------ test/Transforms/OpenMP/replace_globalization.ll | 12 ++++++------ unittests/Analysis/TargetLibraryInfoTest.cpp | 2 +- 7 files changed, 21 insertions(+), 18 deletions(-) diff --git a/include/llvm/Analysis/TargetLibraryInfo.def b/include/llvm/Analysis/TargetLibraryInfo.def index 66fe9641954..ded53617b30 100644 --- a/include/llvm/Analysis/TargetLibraryInfo.def +++ b/include/llvm/Analysis/TargetLibraryInfo.def @@ -332,7 +332,7 @@ TLI_DEFINE_STRING_INTERNAL("__isoc99_sscanf") /// void __kmpc_alloc_shared(size_t nbyte); TLI_DEFINE_ENUM_INTERNAL(__kmpc_alloc_shared) TLI_DEFINE_STRING_INTERNAL("__kmpc_alloc_shared") -/// void __kmpc_free_shared(void *ptr); +/// void __kmpc_free_shared(void *ptr, size_t nbyte); TLI_DEFINE_ENUM_INTERNAL(__kmpc_free_shared) TLI_DEFINE_STRING_INTERNAL("__kmpc_free_shared") /// double __log10_finite(double x); diff --git a/include/llvm/Frontend/OpenMP/OMPKinds.def b/include/llvm/Frontend/OpenMP/OMPKinds.def index 93ab3f6c799..0ef2a5ed90f 100644 --- a/include/llvm/Frontend/OpenMP/OMPKinds.def +++ b/include/llvm/Frontend/OpenMP/OMPKinds.def @@ -430,7 +430,7 @@ __OMP_RTL(__kmpc_nvptx_teams_reduce_nowait_v2, false, Int32, IdentPtr, Int32, __OMP_RTL(__kmpc_shuffle_int64, false, Int64, Int64, Int16, Int16) __OMP_RTL(__kmpc_alloc_shared, false, VoidPtr, SizeTy) -__OMP_RTL(__kmpc_free_shared, false, Void, VoidPtr) +__OMP_RTL(__kmpc_free_shared, false, Void, VoidPtr, SizeTy) __OMP_RTL(__kmpc_begin_sharing_variables, false, Void, VoidPtrPtrPtr, SizeTy) __OMP_RTL(__kmpc_end_sharing_variables, false, Void, ) __OMP_RTL(__kmpc_get_shared_variables, false, Void, VoidPtrPtrPtr) diff --git a/lib/Analysis/MemoryBuiltins.cpp b/lib/Analysis/MemoryBuiltins.cpp index e3d197c482b..68e997656d8 100644 --- a/lib/Analysis/MemoryBuiltins.cpp +++ b/lib/Analysis/MemoryBuiltins.cpp @@ -434,7 +434,6 @@ const CallInst *llvm::extractCallocCall(const Value *I, bool llvm::isLibFreeFunction(const Function *F, const LibFunc TLIFn) { unsigned ExpectedNumParams; if (TLIFn == LibFunc_free || - TLIFn == LibFunc___kmpc_free_shared || // OpenMP Offloading RTL free TLIFn == LibFunc_ZdlPv || // operator delete(void*) TLIFn == LibFunc_ZdaPv || // operator delete[](void*) TLIFn == LibFunc_msvc_delete_ptr32 || // operator delete(void*) @@ -457,7 +456,8 @@ bool llvm::isLibFreeFunction(const Function *F, const LibFunc TLIFn) { TLIFn == LibFunc_msvc_delete_array_ptr32_int || // delete[](void*, uint) TLIFn == LibFunc_msvc_delete_array_ptr64_longlong || // delete[](void*, ulonglong) TLIFn == LibFunc_msvc_delete_array_ptr32_nothrow || // delete[](void*, nothrow) - TLIFn == LibFunc_msvc_delete_array_ptr64_nothrow) // delete[](void*, nothrow) + TLIFn == LibFunc_msvc_delete_array_ptr64_nothrow || // delete[](void*, nothrow) + TLIFn == LibFunc___kmpc_free_shared) // OpenMP Offloading RTL free ExpectedNumParams = 2; else if (TLIFn == LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t || // delete(void*, align_val_t, nothrow) TLIFn == LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t || // delete[](void*, align_val_t, nothrow) diff --git a/lib/Analysis/TargetLibraryInfo.cpp b/lib/Analysis/TargetLibraryInfo.cpp index 812200c1467..4a8818f2e2a 100644 --- a/lib/Analysis/TargetLibraryInfo.cpp +++ b/lib/Analysis/TargetLibraryInfo.cpp @@ -1048,8 +1048,10 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy, case LibFunc_mktime: case LibFunc_times: case LibFunc_vec_free: - case LibFunc___kmpc_free_shared: return (NumParams != 0 && FTy.getParamType(0)->isPointerTy()); + case LibFunc___kmpc_free_shared: + return (NumParams == 2 && FTy.getParamType(0)->isPointerTy() && + IsSizeTTy(FTy.getParamType(1))); case LibFunc_fopen: return (NumParams == 2 && FTy.getReturnType()->isPointerTy() && diff --git a/test/Transforms/OpenMP/remove_globalization.ll b/test/Transforms/OpenMP/remove_globalization.ll index 1ffbceaa985..ebf4e17d9fe 100644 --- a/test/Transforms/OpenMP/remove_globalization.ll +++ b/test/Transforms/OpenMP/remove_globalization.ll @@ -24,6 +24,7 @@ define void @kernel() { ; CHECK-NEXT: call void @bar() #[[ATTR0]] ; CHECK-NEXT: call void @__kmpc_target_deinit(%struct.ident_t* nonnull null, i1 false, i1 true) ; CHECK-NEXT: ret void +; entry: %0 = call i32 @__kmpc_target_init(%struct.ident_t* nonnull null, i1 false, i1 true, i1 true) call void @foo() @@ -42,7 +43,7 @@ define internal void @foo() { entry: %0 = call i8* @__kmpc_alloc_shared(i64 4), !dbg !12 call void @use(i8* %0) - call void @__kmpc_free_shared(i8* %0) + call void @__kmpc_free_shared(i8* %0, i64 4) ret void } @@ -52,13 +53,13 @@ define internal void @bar() { ; CHECK-NEXT: entry: ; CHECK-NEXT: [[TMP0:%.*]] = call i8* @__kmpc_alloc_shared(i64 noundef 4) #[[ATTR0]], !dbg [[DBG8:![0-9]+]] ; CHECK-NEXT: call void @share(i8* nofree writeonly [[TMP0]]) #[[ATTR3:[0-9]+]] -; CHECK-NEXT: call void @__kmpc_free_shared(i8* [[TMP0]]) #[[ATTR0]] +; CHECK-NEXT: call void @__kmpc_free_shared(i8* [[TMP0]], i64 noundef 4) #[[ATTR0]] ; CHECK-NEXT: ret void ; entry: %0 = call i8* @__kmpc_alloc_shared(i64 4), !dbg !13 call void @share(i8* %0) - call void @__kmpc_free_shared(i8* %0) + call void @__kmpc_free_shared(i8* %0, i64 4) ret void } @@ -94,15 +95,15 @@ define void @unused() { entry: %0 = call i8* @__kmpc_alloc_shared(i64 4), !dbg !14 call void @use(i8* %0) - call void @__kmpc_free_shared(i8* %0) + call void @__kmpc_free_shared(i8* %0, i64 4) 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*) +; CHECK: declare void @__kmpc_free_shared(i8* nocapture, i64) +declare void @__kmpc_free_shared(i8*, i64) !llvm.dbg.cu = !{!0} !llvm.module.flags = !{!3, !4, !6, !7} diff --git a/test/Transforms/OpenMP/replace_globalization.ll b/test/Transforms/OpenMP/replace_globalization.ll index 9229f673cac..c26ae61e38c 100644 --- a/test/Transforms/OpenMP/replace_globalization.ll +++ b/test/Transforms/OpenMP/replace_globalization.ll @@ -25,7 +25,7 @@ entry: %x_on_stack = bitcast i8* %x to i32* %0 = bitcast i32* %x_on_stack to i8* call void @use(i8* %0) - call void @__kmpc_free_shared(i8* %x) + call void @__kmpc_free_shared(i8* %x, i64 4) call void @__kmpc_target_deinit(%struct.ident_t* @1, i1 false, i1 true) ret void } @@ -50,7 +50,7 @@ master: %x_on_stack = bitcast i8* %x to [4 x i32]* %0 = bitcast [4 x i32]* %x_on_stack to i8* call void @use(i8* %0) - call void @__kmpc_free_shared(i8* %x) + call void @__kmpc_free_shared(i8* %x, i64 16) br label %exit exit: ret void @@ -67,7 +67,7 @@ master: %y_on_stack = bitcast i8* %y to [4 x i32]* %1 = bitcast [4 x i32]* %y_on_stack to i8* call void @use(i8* %1) - call void @__kmpc_free_shared(i8* %y) + call void @__kmpc_free_shared(i8* %y, i64 4) br label %exit exit: ret void @@ -79,11 +79,11 @@ entry: %0 = icmp eq i32 %call, -1 br i1 %0, label %master, label %exit master: - %y = call i8* @__kmpc_alloc_shared(i64 6), !dbg !12 + %y = call i8* @__kmpc_alloc_shared(i64 24), !dbg !12 %y_on_stack = bitcast i8* %y to [6 x i32]* %1 = bitcast [6 x i32]* %y_on_stack to i8* call void @use(i8* %1) - call void @__kmpc_free_shared(i8* %y) + call void @__kmpc_free_shared(i8* %y, i64 24) br label %exit exit: ret void @@ -98,7 +98,7 @@ entry: declare i8* @__kmpc_alloc_shared(i64) -declare void @__kmpc_free_shared(i8*) +declare void @__kmpc_free_shared(i8*, i64) declare i32 @llvm.nvvm.read.ptx.sreg.tid.x() diff --git a/unittests/Analysis/TargetLibraryInfoTest.cpp b/unittests/Analysis/TargetLibraryInfoTest.cpp index 4f33a1adecf..8c0422aa5cd 100644 --- a/unittests/Analysis/TargetLibraryInfoTest.cpp +++ b/unittests/Analysis/TargetLibraryInfoTest.cpp @@ -589,7 +589,7 @@ TEST_F(TargetLibraryInfoTest, ValidProto) { // These functions are OpenMP Offloading allocation / free routines "declare i8* @__kmpc_alloc_shared(i64)\n" - "declare void @__kmpc_free_shared(i8*)\n" + "declare void @__kmpc_free_shared(i8*, i64)\n" ); for (unsigned FI = 0; FI != LibFunc::NumLibFuncs; ++FI) {