Skip to content

Commit

Permalink
[OpenMP] Add fields for flags in the offload entry descriptor.
Browse files Browse the repository at this point in the history
Summary:
This patch adds two fields to the offload entry descriptor. One field is meant to signal Ctors/Dtors and `link` global variables, and the other is reserved for runtime library use. 

 Currently, these fields are only filled with zeros in the current code generation, but that will change when `declare target` is added. 

The reason, we are adding these fields now is to make the code generation consistent with the runtime library proposal under review in https://reviews.llvm.org/D14031.

Reviewers: ABataev, hfinkel, carlo.bertolli, kkwli0, arpith-jacob, Hahnfeld

Subscribers: cfe-commits, caomhin, jholewinski

Differential Revision: https://reviews.llvm.org/D28298

llvm-svn: 291124
  • Loading branch information
Samuel Antao committed Jan 5, 2017
1 parent 888e289 commit f83efdb
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 50 deletions.
23 changes: 18 additions & 5 deletions clang/lib/CodeGen/CGOpenMPRuntime.cpp
Expand Up @@ -2701,14 +2701,16 @@ void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
"only required for the device "
"code generation.");
OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] =
OffloadEntryInfoTargetRegion(Order, /*Addr=*/nullptr, /*ID=*/nullptr);
OffloadEntryInfoTargetRegion(Order, /*Addr=*/nullptr, /*ID=*/nullptr,
/*Flags=*/0);
++OffloadingEntriesNum;
}

void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
StringRef ParentName, unsigned LineNum,
llvm::Constant *Addr, llvm::Constant *ID) {
llvm::Constant *Addr, llvm::Constant *ID,
int32_t Flags) {
// If we are emitting code for a target, the entry is already initialized,
// only has to be registered.
if (CGM.getLangOpts().OpenMPIsDevice) {
Expand All @@ -2719,9 +2721,10 @@ void CGOpenMPRuntime::OffloadEntriesInfoManagerTy::
assert(Entry.isValid() && "Entry not initialized!");
Entry.setAddress(Addr);
Entry.setID(ID);
Entry.setFlags(Flags);
return;
} else {
OffloadEntryInfoTargetRegion Entry(OffloadingEntriesNum++, Addr, ID);
OffloadEntryInfoTargetRegion Entry(OffloadingEntriesNum++, Addr, ID, Flags);
OffloadEntriesTargetRegion[DeviceID][FileID][ParentName][LineNum] = Entry;
}
}
Expand Down Expand Up @@ -2888,7 +2891,8 @@ CGOpenMPRuntime::createOffloadingBinaryDescriptorRegistration() {
}

void CGOpenMPRuntime::createOffloadEntry(llvm::Constant *ID,
llvm::Constant *Addr, uint64_t Size) {
llvm::Constant *Addr, uint64_t Size,
int32_t Flags) {
StringRef Name = Addr->getName();
auto *TgtOffloadEntryType = cast<llvm::StructType>(
CGM.getTypes().ConvertTypeForMem(getTgtOffloadEntryQTy()));
Expand Down Expand Up @@ -2918,6 +2922,8 @@ void CGOpenMPRuntime::createOffloadEntry(llvm::Constant *ID,
EntryInit.add(AddrPtr);
EntryInit.add(StrPtr);
EntryInit.addInt(CGM.SizeTy, Size);
EntryInit.addInt(CGM.Int32Ty, Flags);
EntryInit.addInt(CGM.Int32Ty, 0);
llvm::GlobalVariable *Entry =
EntryInit.finishAndCreateGlobal(".omp_offloading.entry",
Align,
Expand Down Expand Up @@ -3090,6 +3096,8 @@ QualType CGOpenMPRuntime::getTgtOffloadEntryQTy() {
// // (function or global)
// char *name; // Name of the function or global.
// size_t size; // Size of the entry info (0 if it a function).
// int32_t flags; // Flags associated with the entry, e.g. 'link'.
// int32_t reserved; // Reserved, to use by the runtime library.
// };
if (TgtOffloadEntryQTy.isNull()) {
ASTContext &C = CGM.getContext();
Expand All @@ -3098,6 +3106,10 @@ QualType CGOpenMPRuntime::getTgtOffloadEntryQTy() {
addFieldToRecordDecl(C, RD, C.VoidPtrTy);
addFieldToRecordDecl(C, RD, C.getPointerType(C.CharTy));
addFieldToRecordDecl(C, RD, C.getSizeType());
addFieldToRecordDecl(
C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true));
addFieldToRecordDecl(
C, RD, C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true));
RD->completeDefinition();
TgtOffloadEntryQTy = C.getRecordType(RD);
}
Expand Down Expand Up @@ -4852,7 +4864,8 @@ void CGOpenMPRuntime::emitTargetOutlinedFunctionHelper(

// Register the information for the entry associated with this target region.
OffloadEntriesInfoManager.registerTargetRegionEntryInfo(
DeviceID, FileID, ParentName, Line, OutlinedFn, OutlinedFnID);
DeviceID, FileID, ParentName, Line, OutlinedFn, OutlinedFnID,
/*Flags=*/0);
}

/// discard all CompoundStmts intervening between two constructs
Expand Down
36 changes: 22 additions & 14 deletions clang/lib/CodeGen/CGOpenMPRuntime.h
Expand Up @@ -110,9 +110,9 @@ class CGOpenMPRuntime {
CodeGenModule &CGM;

/// \brief Creates offloading entry for the provided entry ID \a ID,
/// address \a Addr and size \a Size.
/// address \a Addr, size \a Size, and flags \a Flags.
virtual void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
uint64_t Size);
uint64_t Size, int32_t Flags = 0);

/// \brief Helper to emit outlined function for 'target' directive.
/// \param D Directive to emit.
Expand Down Expand Up @@ -245,10 +245,10 @@ class CGOpenMPRuntime {
unsigned OffloadingEntriesNum;

public:
/// \brief Base class of the entries info.
/// Base class of the entries info.
class OffloadEntryInfo {
public:
/// \brief Kind of a given entry. Currently, only target regions are
/// Kind of a given entry. Currently, only target regions are
/// supported.
enum OffloadingEntryInfoKinds : unsigned {
// Entry is a target region.
Expand All @@ -257,17 +257,24 @@ class CGOpenMPRuntime {
OFFLOAD_ENTRY_INFO_INVALID = ~0u
};

OffloadEntryInfo() : Order(~0u), Kind(OFFLOAD_ENTRY_INFO_INVALID) {}
explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order)
: Order(Order), Kind(Kind) {}
OffloadEntryInfo()
: Flags(0), Order(~0u), Kind(OFFLOAD_ENTRY_INFO_INVALID) {}
explicit OffloadEntryInfo(OffloadingEntryInfoKinds Kind, unsigned Order,
int32_t Flags)
: Flags(Flags), Order(Order), Kind(Kind) {}

bool isValid() const { return Order != ~0u; }
unsigned getOrder() const { return Order; }
OffloadingEntryInfoKinds getKind() const { return Kind; }
int32_t getFlags() const { return Flags; }
void setFlags(int32_t NewFlags) { Flags = NewFlags; }
static bool classof(const OffloadEntryInfo *Info) { return true; }

protected:
// \brief Order this entry was emitted.
private:
/// Flags associated with the device global.
int32_t Flags;

/// Order this entry was emitted.
unsigned Order;

OffloadingEntryInfoKinds Kind;
Expand All @@ -292,12 +299,13 @@ class CGOpenMPRuntime {

public:
OffloadEntryInfoTargetRegion()
: OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, ~0u),
: OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, ~0u,
/*Flags=*/0),
Addr(nullptr), ID(nullptr) {}
explicit OffloadEntryInfoTargetRegion(unsigned Order,
llvm::Constant *Addr,
llvm::Constant *ID)
: OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, Order),
llvm::Constant *ID, int32_t Flags)
: OffloadEntryInfo(OFFLOAD_ENTRY_INFO_TARGET_REGION, Order, Flags),
Addr(Addr), ID(ID) {}

llvm::Constant *getAddress() const { return Addr; }
Expand All @@ -321,8 +329,8 @@ class CGOpenMPRuntime {
/// \brief Register target region entry.
void registerTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
StringRef ParentName, unsigned LineNum,
llvm::Constant *Addr,
llvm::Constant *ID);
llvm::Constant *Addr, llvm::Constant *ID,
int32_t Flags);
/// \brief Return true if a target region entry with the provided
/// information exists.
bool hasTargetRegionEntryInfo(unsigned DeviceID, unsigned FileID,
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGOpenMPRuntimeNVPTX.cpp
Expand Up @@ -306,7 +306,7 @@ CGOpenMPRuntimeNVPTX::createNVPTXRuntimeFunction(unsigned Function) {

void CGOpenMPRuntimeNVPTX::createOffloadEntry(llvm::Constant *ID,
llvm::Constant *Addr,
uint64_t Size) {
uint64_t Size, int32_t) {
auto *F = dyn_cast<llvm::Function>(Addr);
// TODO: Add support for global variables on the device after declare target
// support.
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGOpenMPRuntimeNVPTX.h
Expand Up @@ -66,9 +66,9 @@ class CGOpenMPRuntimeNVPTX : public CGOpenMPRuntime {
//

/// \brief Creates offloading entry for the provided entry ID \a ID,
/// address \a Addr and size \a Size.
/// address \a Addr, size \a Size, and flags \a Flags.
void createOffloadEntry(llvm::Constant *ID, llvm::Constant *Addr,
uint64_t Size) override;
uint64_t Size, int32_t Flags = 0) override;

/// \brief Emit outlined function specialized for the Fork-Join
/// programming model for applicable target directives on the NVPTX device.
Expand Down
4 changes: 2 additions & 2 deletions clang/test/OpenMP/target_codegen.cpp
Expand Up @@ -22,11 +22,11 @@

// CHECK-DAG: [[TT:%.+]] = type { i64, i8 }
// CHECK-DAG: [[S1:%.+]] = type { double }
// CHECK-DAG: [[ENTTY:%.+]] = type { i8*, i8*, i[[SZ:32|64]] }
// CHECK-DAG: [[ENTTY:%.+]] = type { i8*, i8*, i[[SZ:32|64]], i32, i32 }
// CHECK-DAG: [[DEVTY:%.+]] = type { i8*, i8*, [[ENTTY]]*, [[ENTTY]]* }
// CHECK-DAG: [[DSCTY:%.+]] = type { i32, [[DEVTY]]*, [[ENTTY]]*, [[ENTTY]]* }

// TCHECK: [[ENTTY:%.+]] = type { i8*, i8*, i{{32|64}} }
// TCHECK: [[ENTTY:%.+]] = type { i8*, i8*, i{{32|64}}, i32, i32 }

// We have 8 target regions, but only 7 that actually will generate offloading
// code, only 6 will have mapped arguments, and only 4 have all-constant map
Expand Down

0 comments on commit f83efdb

Please sign in to comment.