diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPConstants.h b/llvm/include/llvm/Frontend/OpenMP/OMPConstants.h --- a/llvm/include/llvm/Frontend/OpenMP/OMPConstants.h +++ b/llvm/include/llvm/Frontend/OpenMP/OMPConstants.h @@ -29,6 +29,25 @@ namespace omp { LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE(); +/// IDs for all Internal Control Variables (ICVs). +enum class InternalControlVar { +#define ICV_DATA_ENV(Enum, ...) Enum, +#include "llvm/Frontend/OpenMP/OMPKinds.def" +}; + +#define ICV_DATA_ENV(Enum, ...) \ + constexpr auto Enum = omp::InternalControlVar::Enum; +#include "llvm/Frontend/OpenMP/OMPKinds.def" + +enum class ICVInitValue { +#define ICV_DATA_ENV(Enum, Name, EnvVar, Init) Init, +#include "llvm/Frontend/OpenMP/OMPKinds.def" +}; + +#define ICV_DATA_ENV(Enum, Name, EnvVar, Init) \ + constexpr auto Init = omp::ICVInitValue::Init; +#include "llvm/Frontend/OpenMP/OMPKinds.def" + /// IDs for all omp runtime library (RTL) functions. enum class RuntimeFunction { #define OMP_RTL(Enum, ...) Enum, 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 @@ -230,6 +230,7 @@ #define __OMP_TYPE(VarName) OMP_TYPE(VarName, Type::get##VarName##Ty(Ctx)) __OMP_TYPE(Void) +__OMP_TYPE(Int1) __OMP_TYPE(Int8) __OMP_TYPE(Int32) __OMP_TYPE(Int64) @@ -310,6 +311,51 @@ ///} +/// Internal Control Variables information +/// +///{ + +#ifndef ICV_DATA_ENV +#define ICV_DATA_ENV(Enum, Name, EnvVarName, Init) +#endif + +#define __ICV_DATA_ENV(Name, EnvVarName, Init) \ + ICV_DATA_ENV(ICV_##Name, #Name, #EnvVarName, Init) + +__ICV_DATA_ENV(nthreads, OMP_NUM_THREADS, IMPLEMENTATION_DEFINED) +__ICV_DATA_ENV(active_levels, NONE, ZERO) +__ICV_DATA_ENV(cancel, OMP_CANCELLATION, FALSE) +__ICV_DATA_ENV(__last, last, LAST) + +#undef __ICV_DATA_ENV +#undef ICV_DATA_ENV + +#ifndef ICV_RT_SET +#define ICV_RT_SET(Name, RTL) +#endif + +#define __ICV_RT_SET(Name, RTL) ICV_RT_SET(ICV_##Name, OMPRTL_##RTL) + +__ICV_RT_SET(nthreads, omp_set_num_threads) + +#undef __ICV_RT_SET +#undef ICV_RT_SET + +#ifndef ICV_RT_GET +#define ICV_RT_GET(Name, RTL) +#endif + +#define __ICV_RT_GET(Name, RTL) ICV_RT_GET(ICV_##Name, OMPRTL_##RTL) + +__ICV_RT_GET(nthreads, omp_get_max_threads) +__ICV_RT_GET(active_levels, omp_get_active_level) +__ICV_RT_GET(cancel, omp_get_cancellation) + +#undef __ICV_RT_GET +#undef ICV_RT_GET + +///} + /// Runtime library function (and their attributes) /// ///{ 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 @@ -38,6 +38,9 @@ cl::desc("Disable OpenMP specific optimizations."), cl::Hidden, cl::init(false)); +static cl::opt PrintICVValues("openmp-print-icv-values", cl::init(false), + cl::Hidden); + STATISTIC(NumOpenMPRuntimeCallsDeduplicated, "Number of OpenMP runtime calls deduplicated"); STATISTIC(NumOpenMPParallelRegionsDeleted, @@ -63,10 +66,38 @@ OMPBuilder(M) { initializeTypes(M); initializeRuntimeFunctions(); + initializeInternalControlVars(); OMPBuilder.initialize(); } + /// Generic information that describes an internal control variable. + struct InternalControlVarInfo { + /// The kind, as described by InternalControlVar enum. + InternalControlVar Kind; + + /// The name of the ICV. + StringRef Name; + + /// Environment variable associated with this ICV. + StringRef EnvVarName; + + /// Initial value kind. + ICVInitValue InitKind; + + /// Initial value. + ConstantInt *InitValue; + + /// Setter RTL function associated with this ICV. + RuntimeFunction Setter; + + /// Getter RTL function associated with this ICV. + RuntimeFunction Getter; + + /// RTL Function corresponding to the override clause of this ICV + RuntimeFunction Clause; + }; + /// Generic information that describes a runtime function struct RuntimeFunctionInfo { @@ -165,6 +196,49 @@ RuntimeFunction::OMPRTL___last> RFIs; + /// Map from ICV kind to the ICV description. + EnumeratedArray + ICVs; + + /// Helper to initialize all internal control variable information for those + /// defined in OMPKinds.def. + void initializeInternalControlVars() { +#define ICV_RT_SET(_Name, RTL) \ + { \ + auto &ICV = ICVs[_Name]; \ + ICV.Setter = RTL; \ + } +#define ICV_RT_GET(Name, RTL) \ + { \ + auto &ICV = ICVs[Name]; \ + ICV.Getter = RTL; \ + } +#define ICV_DATA_ENV(Enum, _Name, _EnvVarName, Init) \ + { \ + auto &ICV = ICVs[Enum]; \ + ICV.Name = _Name; \ + ICV.Kind = Enum; \ + ICV.InitKind = Init; \ + ICV.EnvVarName = _EnvVarName; \ + switch (ICV.InitKind) { \ + case IMPLEMENTATION_DEFINED: \ + ICV.InitValue = nullptr; \ + break; \ + case ZERO: \ + ICV.InitValue = \ + ConstantInt::get(Type::getInt32Ty(Int32->getContext()), 0); \ + break; \ + case FALSE: \ + ICV.InitValue = ConstantInt::getFalse(Int1->getContext()); \ + break; \ + case LAST: \ + break; \ + } \ + } +#include "llvm/Frontend/OpenMP/OMPKinds.def" + } + /// Returns true if the function declaration \p F matches the runtime /// function types, that is, return type \p RTFRetType, and argument types /// \p RTFArgTypes. @@ -270,6 +344,27 @@ << " functions in a slice with " << OMPInfoCache.ModuleSlice.size() << " functions\n"); + /// Print initial ICV values for testing. + /// FIXME: This should be done from the Attributor once it is added. + if (PrintICVValues) { + InternalControlVar ICVs[] = {ICV_nthreads, ICV_active_levels, ICV_cancel}; + + for (Function *F : OMPInfoCache.ModuleSlice) { + for (auto ICV : ICVs) { + auto ICVInfo = OMPInfoCache.ICVs[ICV]; + auto Remark = [&](OptimizationRemark OR) { + return OR << "OpenMP ICV " << ore::NV("OpenMPICV", ICVInfo.Name) + << " Value: " + << (ICVInfo.InitValue + ? ICVInfo.InitValue->getValue().toString(10, true) + : "IMPLEMENTATION_DEFINED"); + }; + + emitRemarkOnFunction(F, "OpenMPICVTracker", Remark); + } + } + } + Changed |= deduplicateRuntimeCalls(); Changed |= deleteParallelRegions(); @@ -604,6 +699,18 @@ [&]() { return RemarkCB(RemarkKind(DEBUG_TYPE, RemarkName, Inst)); }); } + /// Emit a remark on a function. Since only OptimizationRemark is supporting + /// this, it can't be made generic. + void emitRemarkOnFunction( + Function *F, StringRef RemarkName, + function_ref &&RemarkCB) { + auto &ORE = OREGetter(F); + + ORE.emit([&]() { + return RemarkCB(OptimizationRemark(DEBUG_TYPE, RemarkName, F)); + }); + } + /// The underyling module. Module &M; diff --git a/llvm/test/Transforms/OpenMP/icv_remarks.ll b/llvm/test/Transforms/OpenMP/icv_remarks.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/OpenMP/icv_remarks.ll @@ -0,0 +1,131 @@ +; RUN: opt -passes=openmpopt -pass-remarks=openmp-opt -openmp-print-icv-values -disable-output < %s 2>&1 | FileCheck %s +; RUN: opt -openmpopt -pass-remarks=openmp-opt -openmp-print-icv-values -disable-output < %s 2>&1 | FileCheck %s + +; ModuleID = 'icv_remarks.c' +source_filename = "icv_remarks.c" +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +%struct.ident_t = type { i32, i32, i32, i32, i8* } + +@.str = private unnamed_addr constant [23 x i8] c";unknown;unknown;0;0;;\00", align 1 +@0 = private unnamed_addr constant %struct.ident_t { i32 0, i32 2, i32 0, i32 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i32 0, i32 0) }, align 8 +@1 = private unnamed_addr constant [26 x i8] c";icv_remarks.c;foo;18;1;;\00", align 1 + +; CHECK-DAG: remark: icv_remarks.c:12:0: OpenMP ICV nthreads Value: IMPLEMENTATION_DEFINED +; CHECK-DAG: remark: icv_remarks.c:12:0: OpenMP ICV active_levels Value: 0 +; CHECK-DAG: remark: icv_remarks.c:12:0: OpenMP ICV cancel Value: 0 +define dso_local void @foo(i32 %a) local_unnamed_addr #0 !dbg !17 { +entry: + %.kmpc_loc.addr = alloca %struct.ident_t, align 8 + %0 = bitcast %struct.ident_t* %.kmpc_loc.addr to i8* + call void @llvm.memcpy.p0i8.p0i8.i64(i8* nonnull align 8 dereferenceable(24) %0, i8* nonnull align 8 dereferenceable(24) bitcast (%struct.ident_t* @0 to i8*), i64 16, i1 false) + call void @llvm.dbg.value(metadata i32 %a, metadata !19, metadata !DIExpression()), !dbg !21 + tail call void @omp_set_num_threads(i32 %a) #1, !dbg !22 + %call = tail call i32 @omp_get_max_threads() #1, !dbg !23 + call void @llvm.dbg.value(metadata i32 %call, metadata !20, metadata !DIExpression()), !dbg !21 + tail call void @use(i32 %call) #1, !dbg !24 + %1 = getelementptr inbounds %struct.ident_t, %struct.ident_t* %.kmpc_loc.addr, i64 0, i32 4, !dbg !25 + store i8* getelementptr inbounds ([26 x i8], [26 x i8]* @1, i64 0, i64 0), i8** %1, align 8, !dbg !25, !tbaa !26 + call void (%struct.ident_t*, i32, void (i32*, i32*, ...)*, ...) @__kmpc_fork_call(%struct.ident_t* nonnull %.kmpc_loc.addr, i32 0, void (i32*, i32*, ...)* bitcast (void (i32*, i32*)* @.omp_outlined. to void (i32*, i32*, ...)*)) #1, !dbg !25 + ret void, !dbg !32 +} + +declare !dbg !4 dso_local void @omp_set_num_threads(i32) local_unnamed_addr #1 + +declare !dbg !9 dso_local i32 @omp_get_max_threads() local_unnamed_addr #1 + +declare !dbg !12 dso_local void @use(i32) local_unnamed_addr #2 + +; CHECK-DAG: remark: icv_remarks.c:18:0: OpenMP ICV nthreads Value: IMPLEMENTATION_DEFINED +; CHECK-DAG: remark: icv_remarks.c:18:0: OpenMP ICV active_levels Value: 0 +; CHECK-DAG: remark: icv_remarks.c:18:0: OpenMP ICV cancel Value: 0 +define internal void @.omp_outlined.(i32* noalias nocapture readnone %.global_tid., i32* noalias nocapture readnone %.bound_tid.) #3 !dbg !33 { +entry: + call void @llvm.dbg.value(metadata i32* %.global_tid., metadata !41, metadata !DIExpression()), !dbg !43 + call void @llvm.dbg.value(metadata i32* %.bound_tid., metadata !42, metadata !DIExpression()), !dbg !43 + call void @llvm.dbg.value(metadata i32* undef, metadata !44, metadata !DIExpression()) #1, !dbg !50 + call void @llvm.dbg.value(metadata i32* undef, metadata !47, metadata !DIExpression()) #1, !dbg !50 + tail call void @omp_set_num_threads(i32 10) #1, !dbg !52 + %call.i = tail call i32 @omp_get_max_threads() #1, !dbg !53 + call void @llvm.dbg.value(metadata i32 %call.i, metadata !48, metadata !DIExpression()) #1, !dbg !54 + tail call void @use(i32 %call.i) #1, !dbg !55 + ret void, !dbg !56 +} + +declare void @llvm.memcpy.p0i8.p0i8.i64(i8* noalias nocapture writeonly, i8* noalias nocapture readonly, i64, i1 immarg) #4 + +declare !callback !57 dso_local void @__kmpc_fork_call(%struct.ident_t*, i32, void (i32*, i32*, ...)*, ...) local_unnamed_addr #1 + +declare void @llvm.dbg.value(metadata, metadata, metadata) #5 + +attributes #0 = { nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind } +attributes #2 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #3 = { norecurse nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #4 = { argmemonly nounwind willreturn } +attributes #5 = { nounwind readnone speculatable willreturn } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!13, !14, !15} +!llvm.ident = !{!16} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 11.0.0 (https://github.com/llvm/llvm-project.git 73cea83a6f5ab521edf3cccfc603534776d691ec)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !3, splitDebugInlining: false, nameTableKind: None) +!1 = !DIFile(filename: "icv_remarks.c", directory: "/tmp") +!2 = !{} +!3 = !{!4, !9, !12} +!4 = !DISubprogram(name: "omp_set_num_threads", scope: !5, file: !5, line: 57, type: !6, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2) +!5 = !DIFile(filename: "/usr/local/lib/clang/11.0.0/include/omp.h", directory: "") +!6 = !DISubroutineType(types: !7) +!7 = !{null, !8} +!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!9 = !DISubprogram(name: "omp_get_max_threads", scope: !5, file: !5, line: 67, type: !10, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2) +!10 = !DISubroutineType(types: !11) +!11 = !{!8} +!12 = !DISubprogram(name: "use", scope: !1, file: !1, line: 10, type: !6, flags: DIFlagPrototyped, spFlags: DISPFlagOptimized, retainedNodes: !2) +!13 = !{i32 7, !"Dwarf Version", i32 4} +!14 = !{i32 2, !"Debug Info Version", i32 3} +!15 = !{i32 1, !"wchar_size", i32 4} +!16 = !{!"clang version 11.0.0 (https://github.com/llvm/llvm-project.git 73cea83a6f5ab521edf3cccfc603534776d691ec)"} +!17 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 12, type: !6, scopeLine: 12, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !18) +!18 = !{!19, !20} +!19 = !DILocalVariable(name: "a", arg: 1, scope: !17, file: !1, line: 12, type: !8) +!20 = !DILocalVariable(name: "num", scope: !17, file: !1, line: 15, type: !8) +!21 = !DILocation(line: 0, scope: !17) +!22 = !DILocation(line: 13, column: 3, scope: !17) +!23 = !DILocation(line: 15, column: 13, scope: !17) +!24 = !DILocation(line: 17, column: 3, scope: !17) +!25 = !DILocation(line: 18, column: 1, scope: !17) +!26 = !{!27, !31, i64 16} +!27 = !{!"ident_t", !28, i64 0, !28, i64 4, !28, i64 8, !28, i64 12, !31, i64 16} +!28 = !{!"int", !29, i64 0} +!29 = !{!"omnipotent char", !30, i64 0} +!30 = !{!"Simple C/C++ TBAA"} +!31 = !{!"any pointer", !29, i64 0} +!32 = !DILocation(line: 24, column: 1, scope: !17) +!33 = distinct !DISubprogram(name: ".omp_outlined.", scope: !1, file: !1, line: 18, type: !34, scopeLine: 18, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !40) +!34 = !DISubroutineType(types: !35) +!35 = !{null, !36, !36} +!36 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !37) +!37 = !DIDerivedType(tag: DW_TAG_restrict_type, baseType: !38) +!38 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !39, size: 64) +!39 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !8) +!40 = !{!41, !42} +!41 = !DILocalVariable(name: ".global_tid.", arg: 1, scope: !33, type: !36, flags: DIFlagArtificial) +!42 = !DILocalVariable(name: ".bound_tid.", arg: 2, scope: !33, type: !36, flags: DIFlagArtificial) +!43 = !DILocation(line: 0, scope: !33) +!44 = !DILocalVariable(name: ".global_tid.", arg: 1, scope: !45, type: !36, flags: DIFlagArtificial) +!45 = distinct !DISubprogram(name: ".omp_outlined._debug__", scope: !1, file: !1, line: 19, type: !34, scopeLine: 19, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !46) +!46 = !{!44, !47, !48} +!47 = !DILocalVariable(name: ".bound_tid.", arg: 2, scope: !45, type: !36, flags: DIFlagArtificial) +!48 = !DILocalVariable(name: "num1", scope: !49, file: !1, line: 21, type: !8) +!49 = distinct !DILexicalBlock(scope: !45, file: !1, line: 19, column: 3) +!50 = !DILocation(line: 0, scope: !45, inlinedAt: !51) +!51 = distinct !DILocation(line: 18, column: 1, scope: !33) +!52 = !DILocation(line: 20, column: 5, scope: !49, inlinedAt: !51) +!53 = !DILocation(line: 21, column: 16, scope: !49, inlinedAt: !51) +!54 = !DILocation(line: 0, scope: !49, inlinedAt: !51) +!55 = !DILocation(line: 22, column: 5, scope: !49, inlinedAt: !51) +!56 = !DILocation(line: 18, column: 1, scope: !33) +!57 = !{!58} +!58 = !{i64 2, i64 -1, i64 -1, i1 true}