From b27ff4d07ddcf6aff8aec36aa05e212da0ab6adf Mon Sep 17 00:00:00 2001 From: Alexey Bataev Date: Wed, 4 Mar 2020 16:15:28 -0500 Subject: [PATCH] [OPENMP50]Codegen for 'destroy' clause in depobj directive. If the destroy clause is appplied, the previously allocated memory for the dependency object must be destroyed. --- clang/lib/CodeGen/CGOpenMPRuntime.cpp | 12 ++++++++++++ clang/lib/CodeGen/CGOpenMPRuntime.h | 5 +++++ clang/lib/CodeGen/CGStmtOpenMP.cpp | 5 +++++ clang/test/OpenMP/depobj_codegen.cpp | 6 ++++++ 4 files changed, 28 insertions(+) diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp index ce23434e..0974927 100644 --- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -5318,6 +5318,18 @@ Address CGOpenMPRuntime::emitDependClause( return DependenciesArray; } +void CGOpenMPRuntime::emitDestroyClause(CodeGenFunction &CGF, LValue DepobjLVal, + SourceLocation Loc) { + llvm::Value *DepObjAddr = CGF.EmitLoadOfScalar(DepobjLVal, Loc); + llvm::Value *ThreadID = getThreadID(CGF, Loc); + // Use default allocator. + llvm::Value *Allocator = llvm::ConstantPointerNull::get(CGF.VoidPtrTy); + llvm::Value *Args[] = {ThreadID, DepObjAddr, Allocator}; + + // _kmpc_free(gtid, addr, nullptr); + (void)CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_free), Args); +} + void CGOpenMPRuntime::emitTaskCall(CodeGenFunction &CGF, SourceLocation Loc, const OMPExecutableDirective &D, llvm::Function *TaskFunction, diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.h b/clang/lib/CodeGen/CGOpenMPRuntime.h index 54223d3..fe52646 100644 --- a/clang/lib/CodeGen/CGOpenMPRuntime.h +++ b/clang/lib/CodeGen/CGOpenMPRuntime.h @@ -1786,6 +1786,11 @@ public: CodeGenFunction &CGF, ArrayRef> Dependencies, bool ForDepobj, SourceLocation Loc); + + /// Emits the code to destroy the dependency object provided in depobj + /// directive. + void emitDestroyClause(CodeGenFunction &CGF, LValue DepobjLVal, + SourceLocation Loc); }; /// Class supports emissionof SIMD-only code. diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp index 822542d..2de1fe6 100644 --- a/clang/lib/CodeGen/CGStmtOpenMP.cpp +++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp @@ -3811,6 +3811,11 @@ void CodeGenFunction::EmitOMPDepobjDirective(const OMPDepobjDirective &S) { Address DepAddr = CGM.getOpenMPRuntime().emitDependClause( *this, Dependencies, /*ForDepobj=*/true, DC->getBeginLoc()); EmitStoreOfScalar(DepAddr.getPointer(), DOLVal); + return; + } + if (const auto *DC = S.getSingleClause()) { + CGM.getOpenMPRuntime().emitDestroyClause(*this, DOLVal, DC->getBeginLoc()); + return; } } diff --git a/clang/test/OpenMP/depobj_codegen.cpp b/clang/test/OpenMP/depobj_codegen.cpp index 7a8264a..1d1809b 100644 --- a/clang/test/OpenMP/depobj_codegen.cpp +++ b/clang/test/OpenMP/depobj_codegen.cpp @@ -38,6 +38,7 @@ int main(int argc, char **argv) { } // CHECK-LABEL: @main +// CHECK: [[B_ADDR:%.+]] = alloca i8*, // CHECK: [[GTID:%.+]] = call i32 @__kmpc_global_thread_num( // CHECK: [[DEP_ADDR_VOID:%.+]] = call i8* @__kmpc_alloc(i32 [[GTID]], i64 72, i8* null) // CHECK: [[DEP_ADDR:%.+]] = bitcast i8* [[DEP_ADDR_VOID]] to [3 x %struct.kmp_depend_info]* @@ -61,8 +62,11 @@ int main(int argc, char **argv) { // CHECK: [[BASE_ADDR:%.+]] = getelementptr inbounds [3 x %struct.kmp_depend_info], [3 x %struct.kmp_depend_info]* [[DEP_ADDR]], i{{.+}} 0, i{{.+}} 0 // CHECK: [[DEP:%.+]] = bitcast %struct.kmp_depend_info* [[BASE_ADDR]] to i8* // CHECK: store i8* [[DEP]], i8** [[MAIN_A]], +// CHECK: [[B:%.+]] = load i8*, i8** [[B_ADDR]], +// CHECK: call void @__kmpc_free(i32 [[GTID]], i8* [[B]], i8* null) // CHECK-LABEL: tmain +// CHECK: [[ARGC_ADDR:%.+]] = alloca i8*, // CHECK: [[GTID:%.+]] = call i32 @__kmpc_global_thread_num( // CHECK: [[DEP_ADDR_VOID:%.+]] = call i8* @__kmpc_alloc(i32 [[GTID]], i64 48, i8* null) // CHECK: [[DEP_ADDR:%.+]] = bitcast i8* [[DEP_ADDR_VOID]] to [2 x %struct.kmp_depend_info]* @@ -79,5 +83,7 @@ int main(int argc, char **argv) { // CHECK: [[BASE_ADDR:%.+]] = getelementptr inbounds [2 x %struct.kmp_depend_info], [2 x %struct.kmp_depend_info]* [[DEP_ADDR]], i{{.+}} 0, i{{.+}} 0 // CHECK: [[DEP:%.+]] = bitcast %struct.kmp_depend_info* [[BASE_ADDR]] to i8* // CHECK: store i8* [[DEP]], i8** [[TMAIN_A]], +// CHECK: [[ARGC:%.+]] = load i8*, i8** [[ARGC_ADDR]], +// CHECK: call void @__kmpc_free(i32 [[GTID]], i8* [[ARGC]], i8* null) #endif -- 2.7.4