Skip to content

Commit ceeaa48

Browse files
committed
[OPENMP][NVPTX]Emit default locations as constant with undefined mode.
For the NVPTX target default locations should be emitted as constants + additional info must be emitted in the reserved_2 field of the ident_t structure. The 1st bit controls the execution mode and the 2nd bit controls use of the lightweight runtime. The combination of the bits for Non-SPMD mode + lightweight runtime represents special undefined mode, used outside of the target regions for orphaned directives or functions. Should allow and additional optimization inside of the target regions. llvm-svn: 347425
1 parent 20935e0 commit ceeaa48

File tree

6 files changed

+58
-10
lines changed

6 files changed

+58
-10
lines changed

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,9 @@ createConstantGlobalStructAndAddToParent(CodeGenModule &CGM, QualType Ty,
14671467

14681468
Address CGOpenMPRuntime::getOrCreateDefaultLocation(unsigned Flags) {
14691469
CharUnits Align = CGM.getContext().getTypeAlignInChars(IdentQTy);
1470-
llvm::Value *Entry = OpenMPDefaultLocMap.lookup(Flags);
1470+
unsigned Reserved2Flags = getDefaultLocationReserved2Flags();
1471+
FlagsTy FlagsKey(Flags, Reserved2Flags);
1472+
llvm::Value *Entry = OpenMPDefaultLocMap.lookup(FlagsKey);
14711473
if (!Entry) {
14721474
if (!DefaultOpenMPPSource) {
14731475
// Initialize default location for psource field of ident_t structure of
@@ -1480,18 +1482,18 @@ Address CGOpenMPRuntime::getOrCreateDefaultLocation(unsigned Flags) {
14801482
llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy);
14811483
}
14821484

1483-
llvm::Constant *Data[] = {llvm::ConstantInt::getNullValue(CGM.Int32Ty),
1484-
llvm::ConstantInt::get(CGM.Int32Ty, Flags),
1485-
llvm::ConstantInt::getNullValue(CGM.Int32Ty),
1486-
llvm::ConstantInt::getNullValue(CGM.Int32Ty),
1487-
DefaultOpenMPPSource};
1485+
llvm::Constant *Data[] = {
1486+
llvm::ConstantInt::getNullValue(CGM.Int32Ty),
1487+
llvm::ConstantInt::get(CGM.Int32Ty, Flags),
1488+
llvm::ConstantInt::get(CGM.Int32Ty, Reserved2Flags),
1489+
llvm::ConstantInt::getNullValue(CGM.Int32Ty), DefaultOpenMPPSource};
14881490
llvm::GlobalValue *DefaultOpenMPLocation =
1489-
createGlobalStruct(CGM, IdentQTy, /*IsConstant=*/false, Data, "",
1491+
createGlobalStruct(CGM, IdentQTy, isDefaultLocationConstant(), Data, "",
14901492
llvm::GlobalValue::PrivateLinkage);
14911493
DefaultOpenMPLocation->setUnnamedAddr(
14921494
llvm::GlobalValue::UnnamedAddr::Global);
14931495

1494-
OpenMPDefaultLocMap[Flags] = Entry = DefaultOpenMPLocation;
1496+
OpenMPDefaultLocMap[FlagsKey] = Entry = DefaultOpenMPLocation;
14951497
}
14961498
return Address(Entry, Align);
14971499
}

clang/lib/CodeGen/CGOpenMPRuntime.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,21 @@ class CGOpenMPRuntime {
282282
bool AtCurrentPoint = false);
283283
void clearLocThreadIdInsertPt(CodeGenFunction &CGF);
284284

285+
/// Check if the default location must be constant.
286+
/// Default is false to support OMPT/OMPD.
287+
virtual bool isDefaultLocationConstant() const { return false; }
288+
289+
/// Returns additional flags that can be stored in reserved_2 field of the
290+
/// default location.
291+
virtual unsigned getDefaultLocationReserved2Flags() const { return 0; }
292+
285293
private:
286294
/// Default const ident_t object used for initialization of all other
287295
/// ident_t objects.
288296
llvm::Constant *DefaultOpenMPPSource = nullptr;
297+
using FlagsTy = std::pair<unsigned, unsigned>;
289298
/// Map of flags and corresponding default locations.
290-
typedef llvm::DenseMap<unsigned, llvm::Value *> OpenMPDefaultLocMapTy;
299+
using OpenMPDefaultLocMapTy = llvm::DenseMap<FlagsTy, llvm::Value *>;
291300
OpenMPDefaultLocMapTy OpenMPDefaultLocMap;
292301
Address getOrCreateDefaultLocation(unsigned Flags);
293302

clang/lib/CodeGen/CGOpenMPRuntimeNVPTX.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,26 @@ void CGOpenMPRuntimeNVPTX::emitTargetOutlinedFunction(
19021902
setPropertyExecutionMode(CGM, OutlinedFn->getName(), Mode);
19031903
}
19041904

1905+
namespace {
1906+
LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
1907+
/// Enum for accesseing the reserved_2 field of the ident_t struct.
1908+
enum ModeFlagsTy : unsigned {
1909+
/// Bit set to 1 when in SPMD mode.
1910+
KMP_IDENT_SPMD_MODE = 0x01,
1911+
/// Bit set to 1 when a simplified runtime is used.
1912+
KMP_IDENT_SIMPLE_RT_MODE = 0x02,
1913+
LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/KMP_IDENT_SIMPLE_RT_MODE)
1914+
};
1915+
1916+
/// Special mode Undefined. Is the combination of Non-SPMD mode + SimpleRuntime.
1917+
static const ModeFlagsTy UndefinedMode =
1918+
(~KMP_IDENT_SPMD_MODE) & KMP_IDENT_SIMPLE_RT_MODE;
1919+
} // anonymous namespace
1920+
1921+
unsigned CGOpenMPRuntimeNVPTX::getDefaultLocationReserved2Flags() const {
1922+
return UndefinedMode;
1923+
}
1924+
19051925
CGOpenMPRuntimeNVPTX::CGOpenMPRuntimeNVPTX(CodeGenModule &CGM)
19061926
: CGOpenMPRuntime(CGM, "_", "$") {
19071927
if (!CGM.getLangOpts().OpenMPIsDevice)

clang/lib/CodeGen/CGOpenMPRuntimeNVPTX.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ class CGOpenMPRuntimeNVPTX : public CGOpenMPRuntime {
180180
return "__omp_outlined__";
181181
}
182182

183+
/// Check if the default location must be constant.
184+
/// Constant for NVPTX for better optimization.
185+
bool isDefaultLocationConstant() const override { return true; }
186+
187+
/// Returns additional flags that can be stored in reserved_2 field of the
188+
/// default location.
189+
/// For NVPTX target contains data about SPMD/Non-SPMD execution mode +
190+
/// Full/Lightweight runtime mode. Used for better optimization.
191+
unsigned getDefaultLocationReserved2Flags() const override;
192+
183193
public:
184194
explicit CGOpenMPRuntimeNVPTX(CodeGenModule &CGM);
185195
void clear() override;

clang/test/OpenMP/nvptx_SPMD_codegen.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
#ifndef HEADER
99
#define HEADER
1010

11+
// CHECK-NOT: @__omp_offloading_{{.+}}_exec_mode = weak constant i8 1
12+
// CHECK-DAG: private unnamed_addr constant %struct.ident_t { i32 0, i32 2050, i32 2, i32 0, i8* getelementptr inbounds
13+
// CHECK-DAG: private unnamed_addr constant %struct.ident_t { i32 0, i32 514, i32 2, i32 0, i8* getelementptr inbounds
14+
// CHECK-DAG: private unnamed_addr constant %struct.ident_t { i32 0, i32 2, i32 2, i32 0, i8* getelementptr inbounds
15+
// CHECK-DAG: private unnamed_addr constant %struct.ident_t { i32 0, i32 66, i32 2, i32 0, i8* getelementptr inbounds
1116
// CHECK-NOT: @__omp_offloading_{{.+}}_exec_mode = weak constant i8 1
1217

1318
void foo() {

clang/test/OpenMP/nvptx_target_printf_codegen.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
// expected-no-diagnostics
77
extern int printf(const char *, ...);
88

9+
// CHECK-DAG: private unnamed_addr constant %struct.ident_t { i32 0, i32 2, i32 2, i32 0, i8* getelementptr inbounds
10+
911
// Check a simple call to printf end-to-end.
10-
// CHECK: [[SIMPLE_PRINTF_TY:%[a-zA-Z0-9_]+]] = type { i32, i64, double }
12+
// CHECK-DAG: [[SIMPLE_PRINTF_TY:%[a-zA-Z0-9_]+]] = type { i32, i64, double }
1113
int CheckSimple() {
1214
// CHECK: define {{.*}}void [[T1:@__omp_offloading_.+CheckSimple.+]]_worker()
1315
#pragma omp target

0 commit comments

Comments
 (0)