Skip to content

Commit f3cd223

Browse files
authored
[OpenMP][OpenMPIRBuilder] Add initial changes for SPIR-V target frontend support (#125920)
As Intel is working to add support for SPIR-V OpenMP device offloading in upstream clang/liboffload, we need to modify the OpenMP frontend to allow SPIR-V as well as generate valid IR for SPIR-V. For example, we need the frontend to generate code to define and interact with device globals used in the DeviceRTL. This is the beginning of what I expect will be (many) other changes, but let's get started with something simple. --------- Signed-off-by: Sarnie, Nick <[email protected]>
1 parent 8380b5c commit f3cd223

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

clang/include/clang/Basic/TargetInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,7 @@ class TargetInfo : public TransferrableTargetInfo,
16621662
// access target-specific GPU grid values that must be consistent between
16631663
// host RTL (plugin), deviceRTL and clang.
16641664
virtual const llvm::omp::GV &getGridValue() const {
1665-
llvm_unreachable("getGridValue not implemented on this target");
1665+
return llvm::omp::SPIRVGridValues;
16661666
}
16671667

16681668
/// Retrieve the name of the platform as it is used in the

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,10 @@ void CodeGenModule::createOpenMPRuntime() {
486486
case llvm::Triple::nvptx:
487487
case llvm::Triple::nvptx64:
488488
case llvm::Triple::amdgcn:
489-
assert(getLangOpts().OpenMPIsTargetDevice &&
490-
"OpenMP AMDGPU/NVPTX is only prepared to deal with device code.");
489+
case llvm::Triple::spirv64:
490+
assert(
491+
getLangOpts().OpenMPIsTargetDevice &&
492+
"OpenMP AMDGPU/NVPTX/SPIRV is only prepared to deal with device code.");
491493
OpenMPRuntime.reset(new CGOpenMPRuntimeGPU(*this));
492494
break;
493495
default:
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-linux -fopenmp-targets=spirv64-intel -emit-llvm-bc %s -o %t-host.bc
2+
// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple spirv64-intel -fopenmp-targets=spirv64-intel -emit-llvm %s -fopenmp-is-target-device -fopenmp-host-ir-file-path %t-host.bc -o - | FileCheck %s
3+
4+
// expected-no-diagnostics
5+
6+
// CHECK: @__omp_offloading_{{.*}}_dynamic_environment = weak_odr protected addrspace(1) global %struct.DynamicEnvironmentTy zeroinitializer
7+
// CHECK: @__omp_offloading_{{.*}}_kernel_environment = weak_odr protected addrspace(1) constant %struct.KernelEnvironmentTy
8+
9+
// CHECK: define weak_odr protected spir_kernel void @__omp_offloading_{{.*}}
10+
11+
int main() {
12+
int ret = 0;
13+
#pragma omp target
14+
for(int i = 0; i < 5; i++)
15+
ret++;
16+
return ret;
17+
}

llvm/include/llvm/Frontend/OpenMP/OMPGridValues.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ static constexpr GV NVPTXGridValues = {
120120
128, // GV_Default_WG_Size
121121
};
122122

123+
/// For generic SPIR-V GPUs
124+
static constexpr GV SPIRVGridValues = {
125+
256, // GV_Slot_Size
126+
64, // GV_Warp_Size
127+
(1 << 16), // GV_Max_Teams
128+
440, // GV_Default_Num_Teams
129+
896, // GV_SimpleBufferSize
130+
1024, // GV_Max_WG_Size,
131+
256, // GV_Default_WG_Size
132+
};
133+
123134
} // namespace omp
124135
} // namespace llvm
125136

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ static const omp::GV &getGridValue(const Triple &T, Function *Kernel) {
159159
}
160160
if (T.isNVPTX())
161161
return omp::NVPTXGridValues;
162+
if (T.isSPIRV())
163+
return omp::SPIRVGridValues;
162164
llvm_unreachable("No grid value available for this architecture!");
163165
}
164166

@@ -6472,6 +6474,8 @@ void OpenMPIRBuilder::setOutlinedTargetRegionFunctionAttributes(
64726474
OutlinedFn->setCallingConv(CallingConv::AMDGPU_KERNEL);
64736475
else if (T.isNVPTX())
64746476
OutlinedFn->setCallingConv(CallingConv::PTX_Kernel);
6477+
else if (T.isSPIRV())
6478+
OutlinedFn->setCallingConv(CallingConv::SPIR_KERNEL);
64756479
}
64766480
}
64776481

0 commit comments

Comments
 (0)