Skip to content

[OpenMP][OpenMPIRBuilder] Add initial changes for SPIR-V target frontend support #125920

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

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/TargetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@ class TargetInfo : public TransferrableTargetInfo,
// access target-specific GPU grid values that must be consistent between
// host RTL (plugin), deviceRTL and clang.
virtual const llvm::omp::GV &getGridValue() const {
llvm_unreachable("getGridValue not implemented on this target");
return llvm::omp::SPIRVGridValues;
}

/// Retrieve the name of the platform as it is used in the
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,10 @@ void CodeGenModule::createOpenMPRuntime() {
case llvm::Triple::nvptx:
case llvm::Triple::nvptx64:
case llvm::Triple::amdgcn:
assert(getLangOpts().OpenMPIsTargetDevice &&
"OpenMP AMDGPU/NVPTX is only prepared to deal with device code.");
case llvm::Triple::spirv64:
assert(
getLangOpts().OpenMPIsTargetDevice &&
"OpenMP AMDGPU/NVPTX/SPIRV is only prepared to deal with device code.");
OpenMPRuntime.reset(new CGOpenMPRuntimeGPU(*this));
break;
default:
Expand Down
17 changes: 17 additions & 0 deletions clang/test/OpenMP/spirv_target_codegen_basic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-linux -fopenmp-targets=spirv64-intel -emit-llvm-bc %s -o %t-host.bc
// 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

// expected-no-diagnostics

// CHECK: @__omp_offloading_{{.*}}_dynamic_environment = weak_odr protected addrspace(1) global %struct.DynamicEnvironmentTy zeroinitializer
// CHECK: @__omp_offloading_{{.*}}_kernel_environment = weak_odr protected addrspace(1) constant %struct.KernelEnvironmentTy

// CHECK: define weak_odr protected spir_kernel void @__omp_offloading_{{.*}}

int main() {
int ret = 0;
#pragma omp target
for(int i = 0; i < 5; i++)
ret++;
return ret;
}
11 changes: 11 additions & 0 deletions llvm/include/llvm/Frontend/OpenMP/OMPGridValues.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ static constexpr GV NVPTXGridValues = {
128, // GV_Default_WG_Size
};

/// For generic SPIR-V GPUs
static constexpr GV SPIRVGridValues = {
256, // GV_Slot_Size
64, // GV_Warp_Size
(1 << 16), // GV_Max_Teams
440, // GV_Default_Num_Teams
896, // GV_SimpleBufferSize
1024, // GV_Max_WG_Size,
256, // GV_Default_WG_Size
};

} // namespace omp
} // namespace llvm

Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ static const omp::GV &getGridValue(const Triple &T, Function *Kernel) {
}
if (T.isNVPTX())
return omp::NVPTXGridValues;
if (T.isSPIRV())
return omp::SPIRVGridValues;
llvm_unreachable("No grid value available for this architecture!");
}

Expand Down Expand Up @@ -6470,6 +6472,8 @@ void OpenMPIRBuilder::setOutlinedTargetRegionFunctionAttributes(
OutlinedFn->setCallingConv(CallingConv::AMDGPU_KERNEL);
else if (T.isNVPTX())
OutlinedFn->setCallingConv(CallingConv::PTX_Kernel);
else if (T.isSPIRV())
OutlinedFn->setCallingConv(CallingConv::SPIR_KERNEL);
}
}

Expand Down