-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[clang][OMPIRBuilder] Use default target AS to create types #158152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
std::optional<omp::GV> GridValue; | ||
|
||
/// When compilation is being done for the OpenMP host (i.e. `IsTargetDevice = | ||
/// false`), this contains the list of offloading triples associated, if any. | ||
SmallVector<Triple> TargetTriples; | ||
|
||
// Default address space for the target. | ||
unsigned DefaultTargetAS = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't use std::optional
here because we create OpenMPIRBuilder
directly in a ton of unit tests directly and I didn't want to update them all, and also I think that since we already assume the default AS
is zero we can continue doing that in cases where we don't set it from clang
.
Signed-off-by: Sarnie, Nick <nick.sarnie@intel.com>
@llvm/pr-subscribers-flang-openmp @llvm/pr-subscribers-clang-codegen Author: Nick Sarnie (sarnex) ChangesCurrently we assume that Pass down the AS from After this change, we finally generate fully valid SPIR-V for a basic OpenMP Offloading example. Full diff: https://github.com/llvm/llvm-project/pull/158152.diff 5 Files Affected:
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index e80aa1592f252..a503aaf613e30 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -1037,12 +1037,15 @@ CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM)
CGM.getLangOpts().OpenMPOffloadMandatory,
/*HasRequiresReverseOffload*/ false, /*HasRequiresUnifiedAddress*/ false,
hasRequiresUnifiedSharedMemory(), /*HasRequiresDynamicAllocators*/ false);
+ Config.setDefaultTargetAS(
+ CGM.getContext().getTargetInfo().getTargetAddressSpace(LangAS::Default));
+
+ OMPBuilder.setConfig(Config);
OMPBuilder.initialize();
OMPBuilder.loadOffloadInfoMetadata(*CGM.getFileSystem(),
CGM.getLangOpts().OpenMPIsTargetDevice
? CGM.getLangOpts().OMPHostIRFile
: StringRef{});
- OMPBuilder.setConfig(Config);
// The user forces the compiler to behave as if omp requires
// unified_shared_memory was given.
diff --git a/clang/test/OpenMP/spirv_locstr.cpp b/clang/test/OpenMP/spirv_locstr.cpp
index 20d9c9d2f7393..80f05029dd8a4 100644
--- a/clang/test/OpenMP/spirv_locstr.cpp
+++ b/clang/test/OpenMP/spirv_locstr.cpp
@@ -4,7 +4,7 @@
// expected-no-diagnostics
// CHECK: @[[#LOC:]] = private unnamed_addr addrspace(1) constant [23 x i8] c";unknown;unknown;0;0;;\00", align 1
-// CHECK: = private unnamed_addr addrspace(1) constant %struct.ident_t { i32 0, i32 2, i32 0, i32 {{.*}}, ptr addrspacecast (ptr addrspace(1) @[[#LOC]] to ptr) }, align 8
+// CHECK: = private unnamed_addr addrspace(1) constant %struct.ident_t { i32 0, i32 2, i32 0, i32 {{.*}}, ptr addrspace(4) addrspacecast (ptr addrspace(1) @[[#LOC]] to ptr addrspace(4)) }, align 8
int main() {
int ret = 0;
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
index cc1177ba3d11c..f43ef932e965a 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -125,16 +125,19 @@ class OpenMPIRBuilderConfig {
/// First separator used between the initial two parts of a name.
std::optional<StringRef> FirstSeparator;
- /// Separator used between all of the rest consecutive parts of s name
+ /// Separator used between all of the rest consecutive parts of s name.
std::optional<StringRef> Separator;
- // Grid Value for the GPU target
+ // Grid Value for the GPU target.
std::optional<omp::GV> GridValue;
/// When compilation is being done for the OpenMP host (i.e. `IsTargetDevice =
/// false`), this contains the list of offloading triples associated, if any.
SmallVector<Triple> TargetTriples;
+ // Default address space for the target.
+ unsigned DefaultTargetAS = 0;
+
LLVM_ABI OpenMPIRBuilderConfig();
LLVM_ABI OpenMPIRBuilderConfig(bool IsTargetDevice, bool IsGPU,
bool OpenMPOffloadMandatory,
@@ -165,6 +168,8 @@ class OpenMPIRBuilderConfig {
return *GridValue;
}
+ unsigned getDefaultTargetAS() const { return DefaultTargetAS; }
+
bool hasRequiresFlags() const { return RequiresFlags; }
LLVM_ABI bool hasRequiresReverseOffload() const;
LLVM_ABI bool hasRequiresUnifiedAddress() const;
@@ -202,6 +207,7 @@ class OpenMPIRBuilderConfig {
void setFirstSeparator(StringRef FS) { FirstSeparator = FS; }
void setSeparator(StringRef S) { Separator = S; }
void setGridValue(omp::GV G) { GridValue = G; }
+ void setDefaultTargetAS(unsigned AS) { DefaultTargetAS = AS; }
LLVM_ABI void setHasRequiresReverseOffload(bool Value);
LLVM_ABI void setHasRequiresUnifiedAddress(bool Value);
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPKinds.def b/llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
index 71f041ac138e3..01ca8da759ef7 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
@@ -26,7 +26,7 @@
#endif
#define __OMP_TYPE(VarName) OMP_TYPE(VarName, Type::get##VarName##Ty(Ctx))
-#define __OMP_PTR_TYPE(VarName) OMP_TYPE(VarName, PointerType::get(Ctx, 0))
+#define __OMP_PTR_TYPE(VarName) OMP_TYPE(VarName, PointerType::get(Ctx, DefaultTargetAS))
__OMP_TYPE(Void)
__OMP_TYPE(Int1)
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index c955ecd403633..d1f78c32596ba 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -10056,19 +10056,20 @@ OpenMPIRBuilder::createOffloadMapnames(SmallVectorImpl<llvm::Constant *> &Names,
void OpenMPIRBuilder::initializeTypes(Module &M) {
LLVMContext &Ctx = M.getContext();
StructType *T;
+ unsigned DefaultTargetAS = Config.getDefaultTargetAS();
#define OMP_TYPE(VarName, InitValue) VarName = InitValue;
#define OMP_ARRAY_TYPE(VarName, ElemTy, ArraySize) \
VarName##Ty = ArrayType::get(ElemTy, ArraySize); \
- VarName##PtrTy = PointerType::getUnqual(Ctx);
+ VarName##PtrTy = PointerType::get(Ctx, DefaultTargetAS);
#define OMP_FUNCTION_TYPE(VarName, IsVarArg, ReturnType, ...) \
VarName = FunctionType::get(ReturnType, {__VA_ARGS__}, IsVarArg); \
- VarName##Ptr = PointerType::getUnqual(Ctx);
+ VarName##Ptr = PointerType::get(Ctx, DefaultTargetAS);
#define OMP_STRUCT_TYPE(VarName, StructName, Packed, ...) \
T = StructType::getTypeByName(Ctx, StructName); \
if (!T) \
T = StructType::create(Ctx, {__VA_ARGS__}, StructName, Packed); \
VarName = T; \
- VarName##Ptr = PointerType::getUnqual(Ctx);
+ VarName##Ptr = PointerType::get(Ctx, DefaultTargetAS);
#include "llvm/Frontend/OpenMP/OMPKinds.def"
}
|
@llvm/pr-subscribers-clang Author: Nick Sarnie (sarnex) ChangesCurrently we assume that Pass down the AS from After this change, we finally generate fully valid SPIR-V for a basic OpenMP Offloading example. Full diff: https://github.com/llvm/llvm-project/pull/158152.diff 5 Files Affected:
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index e80aa1592f252..a503aaf613e30 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -1037,12 +1037,15 @@ CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM)
CGM.getLangOpts().OpenMPOffloadMandatory,
/*HasRequiresReverseOffload*/ false, /*HasRequiresUnifiedAddress*/ false,
hasRequiresUnifiedSharedMemory(), /*HasRequiresDynamicAllocators*/ false);
+ Config.setDefaultTargetAS(
+ CGM.getContext().getTargetInfo().getTargetAddressSpace(LangAS::Default));
+
+ OMPBuilder.setConfig(Config);
OMPBuilder.initialize();
OMPBuilder.loadOffloadInfoMetadata(*CGM.getFileSystem(),
CGM.getLangOpts().OpenMPIsTargetDevice
? CGM.getLangOpts().OMPHostIRFile
: StringRef{});
- OMPBuilder.setConfig(Config);
// The user forces the compiler to behave as if omp requires
// unified_shared_memory was given.
diff --git a/clang/test/OpenMP/spirv_locstr.cpp b/clang/test/OpenMP/spirv_locstr.cpp
index 20d9c9d2f7393..80f05029dd8a4 100644
--- a/clang/test/OpenMP/spirv_locstr.cpp
+++ b/clang/test/OpenMP/spirv_locstr.cpp
@@ -4,7 +4,7 @@
// expected-no-diagnostics
// CHECK: @[[#LOC:]] = private unnamed_addr addrspace(1) constant [23 x i8] c";unknown;unknown;0;0;;\00", align 1
-// CHECK: = private unnamed_addr addrspace(1) constant %struct.ident_t { i32 0, i32 2, i32 0, i32 {{.*}}, ptr addrspacecast (ptr addrspace(1) @[[#LOC]] to ptr) }, align 8
+// CHECK: = private unnamed_addr addrspace(1) constant %struct.ident_t { i32 0, i32 2, i32 0, i32 {{.*}}, ptr addrspace(4) addrspacecast (ptr addrspace(1) @[[#LOC]] to ptr addrspace(4)) }, align 8
int main() {
int ret = 0;
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
index cc1177ba3d11c..f43ef932e965a 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -125,16 +125,19 @@ class OpenMPIRBuilderConfig {
/// First separator used between the initial two parts of a name.
std::optional<StringRef> FirstSeparator;
- /// Separator used between all of the rest consecutive parts of s name
+ /// Separator used between all of the rest consecutive parts of s name.
std::optional<StringRef> Separator;
- // Grid Value for the GPU target
+ // Grid Value for the GPU target.
std::optional<omp::GV> GridValue;
/// When compilation is being done for the OpenMP host (i.e. `IsTargetDevice =
/// false`), this contains the list of offloading triples associated, if any.
SmallVector<Triple> TargetTriples;
+ // Default address space for the target.
+ unsigned DefaultTargetAS = 0;
+
LLVM_ABI OpenMPIRBuilderConfig();
LLVM_ABI OpenMPIRBuilderConfig(bool IsTargetDevice, bool IsGPU,
bool OpenMPOffloadMandatory,
@@ -165,6 +168,8 @@ class OpenMPIRBuilderConfig {
return *GridValue;
}
+ unsigned getDefaultTargetAS() const { return DefaultTargetAS; }
+
bool hasRequiresFlags() const { return RequiresFlags; }
LLVM_ABI bool hasRequiresReverseOffload() const;
LLVM_ABI bool hasRequiresUnifiedAddress() const;
@@ -202,6 +207,7 @@ class OpenMPIRBuilderConfig {
void setFirstSeparator(StringRef FS) { FirstSeparator = FS; }
void setSeparator(StringRef S) { Separator = S; }
void setGridValue(omp::GV G) { GridValue = G; }
+ void setDefaultTargetAS(unsigned AS) { DefaultTargetAS = AS; }
LLVM_ABI void setHasRequiresReverseOffload(bool Value);
LLVM_ABI void setHasRequiresUnifiedAddress(bool Value);
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPKinds.def b/llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
index 71f041ac138e3..01ca8da759ef7 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPKinds.def
@@ -26,7 +26,7 @@
#endif
#define __OMP_TYPE(VarName) OMP_TYPE(VarName, Type::get##VarName##Ty(Ctx))
-#define __OMP_PTR_TYPE(VarName) OMP_TYPE(VarName, PointerType::get(Ctx, 0))
+#define __OMP_PTR_TYPE(VarName) OMP_TYPE(VarName, PointerType::get(Ctx, DefaultTargetAS))
__OMP_TYPE(Void)
__OMP_TYPE(Int1)
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index c955ecd403633..d1f78c32596ba 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -10056,19 +10056,20 @@ OpenMPIRBuilder::createOffloadMapnames(SmallVectorImpl<llvm::Constant *> &Names,
void OpenMPIRBuilder::initializeTypes(Module &M) {
LLVMContext &Ctx = M.getContext();
StructType *T;
+ unsigned DefaultTargetAS = Config.getDefaultTargetAS();
#define OMP_TYPE(VarName, InitValue) VarName = InitValue;
#define OMP_ARRAY_TYPE(VarName, ElemTy, ArraySize) \
VarName##Ty = ArrayType::get(ElemTy, ArraySize); \
- VarName##PtrTy = PointerType::getUnqual(Ctx);
+ VarName##PtrTy = PointerType::get(Ctx, DefaultTargetAS);
#define OMP_FUNCTION_TYPE(VarName, IsVarArg, ReturnType, ...) \
VarName = FunctionType::get(ReturnType, {__VA_ARGS__}, IsVarArg); \
- VarName##Ptr = PointerType::getUnqual(Ctx);
+ VarName##Ptr = PointerType::get(Ctx, DefaultTargetAS);
#define OMP_STRUCT_TYPE(VarName, StructName, Packed, ...) \
T = StructType::getTypeByName(Ctx, StructName); \
if (!T) \
T = StructType::create(Ctx, {__VA_ARGS__}, StructName, Packed); \
VarName = T; \
- VarName##Ptr = PointerType::getUnqual(Ctx);
+ VarName##Ptr = PointerType::get(Ctx, DefaultTargetAS);
#include "llvm/Frontend/OpenMP/OMPKinds.def"
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes LGTM thanks.
if you are trying to enable SPIR-V more generally, you will also need to set this in mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
to fix the other upstream user of OMPIRBuilder. There are already code examples in that file showing how to fetch the default address space from the data layout string in the MILR module, but please do ping me if I can help.
Thanks Tom! In my case I'm never going through mlir yet, but I should make it work, so I will make a separate PR quickly after this to fix MLIR and add you as a reviewer. |
Extension of #158152 for MLIR. --------- Signed-off-by: Sarnie, Nick <nick.sarnie@intel.com>
…er (#158689) Extension of llvm/llvm-project#158152 for MLIR. --------- Signed-off-by: Sarnie, Nick <nick.sarnie@intel.com>
Currently we assume that
0
is the default AS, which is usually true, but it isn't forSPIR-V
.Pass down the AS from
clang
and use it to create types.After this change, we finally generate fully valid SPIR-V for a basic OpenMP Offloading example.