-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang][Driver] Support for the SPIR-V backend in the new driver when compiling HIP #167543
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
base: main
Are you sure you want to change the base?
Changes from all commits
0c3a341
db0f20b
8f86bda
af8f210
46bd79c
86491a8
5f452ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -159,10 +159,9 @@ void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA, | |
|
|
||
| // For SPIR-V the inputs for the job are device AMDGCN SPIR-V flavoured bitcode | ||
| // and the output is either a compiled SPIR-V binary or bitcode (-emit-llvm). It | ||
| // calls llvm-link and then the llvm-spirv translator. Once the SPIR-V BE will | ||
| // be promoted from experimental, we will switch to using that. TODO: consider | ||
| // if we want to run any targeted optimisations over IR here, over generic | ||
| // SPIR-V. | ||
| // calls llvm-link and then the llvm-spirv translator or the SPIR-V BE. | ||
| // TODO: consider if we want to run any targeted optimisations over IR here, | ||
| // over generic SPIR-V. | ||
| void AMDGCN::Linker::constructLinkAndEmitSpirvCommand( | ||
| Compilation &C, const JobAction &JA, const InputInfoList &Inputs, | ||
| const InputInfo &Output, const llvm::opt::ArgList &Args) const { | ||
|
|
@@ -173,17 +172,40 @@ void AMDGCN::Linker::constructLinkAndEmitSpirvCommand( | |
| const char *LinkedBCFilePath = HIP::getTempFile(C, LinkedBCFilePrefix, "bc"); | ||
| InputInfo LinkedBCFile(&JA, LinkedBCFilePath, Output.getBaseInput()); | ||
|
|
||
| bool UseSPIRVBackend = | ||
| Args.hasFlag(options::OPT_use_spirv_backend, | ||
| options::OPT_no_use_spirv_backend, /*Default=*/false); | ||
|
|
||
| constructLlvmLinkCommand(C, JA, Inputs, LinkedBCFile, Args); | ||
|
|
||
| // Emit SPIR-V binary. | ||
| llvm::opt::ArgStringList TrArgs{ | ||
| "--spirv-max-version=1.6", | ||
| "--spirv-ext=+all", | ||
| "--spirv-allow-unknown-intrinsics", | ||
| "--spirv-lower-const-expr", | ||
| "--spirv-preserve-auxdata", | ||
| "--spirv-debug-info-version=nonsemantic-shader-200"}; | ||
| SPIRV::constructTranslateCommand(C, *this, JA, Output, LinkedBCFile, TrArgs); | ||
| if (UseSPIRVBackend) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jhuber6 This PR is designed for the new driver and this handling here is required for the new driver when I'm writing this comment to make a question for the old driver. The old driver always builds SPIR-V in the linking step (this function). This new code for the new driver works out-of-the-box for the old driver. This new code enables Would it be OK to let the old driver use this new code to handle |
||
| // This code handles the case in the new driver when --offload-device-only | ||
| // is unset and clang-linker-wrapper forwards the bitcode that must be | ||
| // compiled to SPIR-V. | ||
|
|
||
| llvm::opt::ArgStringList CmdArgs; | ||
| const char *Triple = | ||
| C.getArgs().MakeArgString("-triple=spirv64-amd-amdhsa"); | ||
|
|
||
| CmdArgs.append({"-cc1", Triple, "-emit-obj", "-disable-llvm-optzns", | ||
| LinkedBCFile.getFilename(), "-o", Output.getFilename()}); | ||
|
|
||
| const char *Exec = getToolChain().getDriver().getClangProgramPath(); | ||
| C.addCommand(std::make_unique<Command>(JA, *this, | ||
| ResponseFileSupport::None(), Exec, | ||
| CmdArgs, LinkedBCFile, Output)); | ||
| } else { | ||
| // Emit SPIR-V binary using the translator | ||
| llvm::opt::ArgStringList TrArgs{ | ||
| "--spirv-max-version=1.6", | ||
| "--spirv-ext=+all", | ||
| "--spirv-allow-unknown-intrinsics", | ||
| "--spirv-lower-const-expr", | ||
| "--spirv-preserve-auxdata", | ||
| "--spirv-debug-info-version=nonsemantic-shader-200"}; | ||
| SPIRV::constructTranslateCommand(C, *this, JA, Output, LinkedBCFile, | ||
| TrArgs); | ||
| } | ||
| } | ||
|
|
||
| // For amdgcn the inputs of the linker job are device bitcode and output is | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // RUN: %clang --offload-new-driver --target=x86_64-unknown-linux-gnu --offload-arch=amdgcnspirv \ | ||
| // RUN: -nogpuinc -nogpulib -x hip %s -save-temps \ | ||
| // RUN: -use-spirv-backend -ccc-print-bindings \ | ||
| // RUN: 2>&1 | FileCheck %s --check-prefixes=CHECK-SPIRV-BASE,CHECK-SPIRV | ||
|
|
||
| // RUN: %clang --offload-new-driver --target=x86_64-unknown-linux-gnu --offload-arch=amdgcnspirv \ | ||
| // RUN: -nogpuinc -nogpulib -x hip %s -save-temps \ | ||
| // RUN: -use-spirv-backend -fgpu-rdc -ccc-print-bindings \ | ||
| // RUN: 2>&1 | FileCheck %s --check-prefixes=CHECK-SPIRV-BASE,CHECK-SPIRV-RDC | ||
|
|
||
| // CHECK-SPIRV-BASE: # "spirv64-amd-amdhsa" - "clang", inputs: ["[[INPUT:.+]]"], output: "[[HIPI:.+\.hipi]]" | ||
| // CHECK-SPIRV-BASE: # "spirv64-amd-amdhsa" - "clang", inputs: ["[[HIPI]]"], output: "[[SPV_BC:.+\.bc]]" | ||
| // CHECK-SPIRV: # "spirv64-amd-amdhsa" - "Offload::Packager", inputs: ["[[SPV_BC]]"], output: "[[HIP_OUT:.+\.out]]" | ||
| // CHECK-SPIRV: # "spirv64-amd-amdhsa" - "Offload::Linker", inputs: ["[[HIP_OUT]]"], output: "[[HIPFB:.+\.hipfb]]" | ||
| // CHECK-SPIRV-RDC: # "x86_64-unknown-linux-gnu" - "Offload::Packager", inputs: ["[[SPV_BC]]"], output: "[[HIP_OUT:.+\.out]]" | ||
| // CHECK-SPIRV-BASE: # "x86_64-unknown-linux-gnu" - "clang", inputs: ["[[INPUT]]"], output: "[[HIPI:.+\.hipi]]" | ||
| // CHECK-SPIRV: # "x86_64-unknown-linux-gnu" - "clang", inputs: ["[[HIPI]]", "[[HIPFB]]"], output: "[[x86_BC:.+\.bc]]" | ||
| // CHECK-SPIRV-RDC: # "x86_64-unknown-linux-gnu" - "clang", inputs: ["[[HIPI]]", "[[HIP_OUT]]"], output: "[[x86_BC:.+\.bc]]" | ||
| // CHECK-SPIRV-BASE: # "x86_64-unknown-linux-gnu" - "clang", inputs: ["[[x86_BC]]"], output: "[[x86_S:.+\.s]]" | ||
| // CHECK-SPIRV-BASE: # "x86_64-unknown-linux-gnu" - "clang::as", inputs: ["[[x86_S]]"], output: "[[x86_O:.+\.o]]" | ||
| // CHECK-SPIRV-BASE: # "x86_64-unknown-linux-gnu" - "Offload::Linker", inputs: ["[[x86_O]]"], output: "{{.+\.out}}" | ||
|
|
||
| // CHECK-SPIRV # "x86_64-unknown-linux-gnu" - "Offload::Linker", inputs: ["[[x86_O]]"], output: "[[x86_O:.+\.o]]" | ||
| // CHECK-SPIRV # "x86_64-unknown-linux-gnu" - "GNU::Linker", inputs: ["[[x86_O]]"], output: "{{.+\.out}}" | ||
|
|
||
| // RUN: %clang --offload-new-driver --target=x86_64-unknown-linux-gnu --offload-arch=amdgcnspirv \ | ||
| // RUN: -nogpuinc -nogpulib -x hip %s -save-temps \ | ||
| // RUN: -use-spirv-backend --offload-device-only -ccc-print-bindings \ | ||
| // RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-SPIRV-OFFLOAD-DEVICE-ONLY | ||
|
|
||
| // CHECK-SPIRV-OFFLOAD-DEVICE-ONLY: # "spirv64-amd-amdhsa" - "clang", inputs: ["[[INPUT:.+]]"], output: "[[HIPI:.+\.hipi]]" | ||
| // CHECK-SPIRV-OFFLOAD-DEVICE-ONLY: # "spirv64-amd-amdhsa" - "clang", inputs: ["[[HIPI]]"], output: "[[SPV_BC:.+\.bc]]" | ||
| // CHECK-SPIRV-OFFLOAD-DEVICE-ONLY: # "spirv64-amd-amdhsa" - "clang", inputs: ["[[SPV_BC]]"], output: "[[SPV_OUT:.+\.out]]" | ||
| // CHECK-SPIRV-OFFLOAD-DEVICE-ONLY: # "spirv64-amd-amdhsa" - "AMDGCN::Linker", inputs: ["[[SPV_OUT]]"], output: "{{.+\.hipfb}}" | ||
|
|
||
| // RUN: %clang --offload-new-driver --target=x86_64-unknown-linux-gnu --offload-arch=amdgcnspirv \ | ||
| // RUN: -nogpuinc -nogpulib -x hip %s -save-temps \ | ||
| // RUN: -use-spirv-backend --offload-device-only -fgpu-rdc -ccc-print-bindings \ | ||
| // RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-SPIRV-OFFLOAD-DEVICE-ONLY-RDC | ||
|
|
||
| // CHECK-SPIRV-OFFLOAD-DEVICE-ONLY-RDC: # "spirv64-amd-amdhsa" - "clang", inputs: ["[[INPUT:.+]]"], output: "[[HIPI:.+\.hipi]]" | ||
| // CHECK-SPIRV-OFFLOAD-DEVICE-ONLY-RDC: # "spirv64-amd-amdhsa" - "clang", inputs: ["[[HIPI]]"], output: "[[SPV_BC:.+\.bc]]" | ||
| // CHECK-SPIRV-OFFLOAD-DEVICE-ONLY-RDC: # "spirv64-amd-amdhsa" - "clang", inputs: ["[[SPV_BC]]"], output: "{{.+}}" | ||
|
|
||
| // RUN: %clang --offload-new-driver --target=x86_64-unknown-linux-gnu --offload-arch=amdgcnspirv \ | ||
| // RUN: -nogpuinc -nogpulib -x hip %s -save-temps \ | ||
| // RUN: -use-spirv-backend --offload-device-only -S -fgpu-rdc -ccc-print-bindings \ | ||
| // RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-SPIRV-OFFLOAD-DEVICE-ONLY-RDC | ||
|
|
||
| // RUN: %clang --offload-new-driver --target=x86_64-unknown-linux-gnu --offload-arch=amdgcnspirv \ | ||
| // RUN: -nogpuinc -nogpulib -x hip %s -save-temps \ | ||
| // RUN: -use-spirv-backend --offload-device-only -S -ccc-print-bindings \ | ||
| // RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-SPIRV-TEXTUAL-OFFLOAD-DEVICE-ONLY | ||
|
|
||
| // CHECK-SPIRV-TEXTUAL-OFFLOAD-DEVICE-ONLY: # "spirv64-amd-amdhsa" - "clang", inputs: ["[[INPUT:.+]]"], output: "[[HIPI:.+\.hipi]]" | ||
| // CHECK-SPIRV-TEXTUAL-OFFLOAD-DEVICE-ONLY: # "spirv64-amd-amdhsa" - "clang", inputs: ["[[HIPI]]"], output: "[[SPV_BC:.+\.bc]]" | ||
| // CHECK-SPIRV-TEXTUAL-OFFLOAD-DEVICE-ONLY: # "spirv64-amd-amdhsa" - "clang", inputs: ["[[SPV_BC]]"], output: "{{.+\.s}}" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // This test case validates the behavior of -use-spirv-backend | ||
|
|
||
| // --offload-device-only is always set --- testing interactions with -S and -fgpu-rdc | ||
|
|
||
| // RUN: %clang --offload-new-driver --target=x86_64-unknown-linux-gnu --offload-arch=amdgcnspirv \ | ||
| // RUN: -nogpuinc -nogpulib -### -x hip %s -save-temps \ | ||
| // RUN: -use-spirv-backend --offload-device-only -S \ | ||
| // RUN: 2>&1 | FileCheck %s --check-prefixes=CHECK-SPIRV-TRANSLATOR,CHECK-SPIRV-BACKEND-TEXTUAL | ||
|
|
||
| // RUN: %clang --offload-new-driver --target=x86_64-unknown-linux-gnu --offload-arch=amdgcnspirv \ | ||
| // RUN: -nogpuinc -nogpulib -### -x hip %s -save-temps \ | ||
| // RUN: -use-spirv-backend --offload-device-only \ | ||
| // RUN: 2>&1 | FileCheck %s --check-prefixes=CHECK-SPIRV-TRANSLATOR,CHECK-SPIRV-BACKEND-BINARY | ||
|
|
||
| // The new driver's behavior is to emit LLVM IR for --offload-device-only and -fgpu-rdc (independently of SPIR-V). | ||
| // RUN: %clang --offload-new-driver --target=x86_64-unknown-linux-gnu --offload-arch=amdgcnspirv \ | ||
| // RUN: -### -nogpuinc -nogpulib -x hip %s -save-temps \ | ||
| // RUN: -use-spirv-backend --offload-device-only -S -fgpu-rdc \ | ||
| // RUN: 2>&1 | FileCheck %s --check-prefixes=CHECK-SPIRV-TRANSLATOR,CHECK-SPIRV-BACKEND-LL,CHECK-FGPU-RDC | ||
|
|
||
| // The new driver's behavior is to emit LLVM IR for --offload-device-only and -fgpu-rdc (independently of SPIR-V). | ||
| // RUN: %clang --offload-new-driver --target=x86_64-unknown-linux-gnu --offload-arch=amdgcnspirv \ | ||
| // RUN: -nogpuinc -nogpulib -### -x hip %s -save-temps \ | ||
| // RUN: -use-spirv-backend --offload-device-only -fgpu-rdc \ | ||
| // RUN: 2>&1 | FileCheck %s --check-prefixes=CHECK-SPIRV-TRANSLATOR,CHECK-SPIRV-BACKEND-BC,CHECK-FGPU-RDC | ||
|
|
||
| // --offload-device-only is always unset --- testing interactions with -S and -fgpu-rdc | ||
|
|
||
| // RUN: %clang --offload-new-driver --target=x86_64-unknown-linux-gnu --offload-arch=amdgcnspirv \ | ||
| // RUN: -nogpuinc -nogpulib -### -x hip %s -save-temps \ | ||
| // RUN: -use-spirv-backend -S -fgpu-rdc \ | ||
| // RUN: 2>&1 | FileCheck %s --check-prefixes=CHECK-SPIRV-TRANSLATOR,CHECK-SPIRV-BACKEND-BC,CHECK-FGPU-RDC | ||
|
|
||
| // RUN: %clang --offload-new-driver --target=x86_64-unknown-linux-gnu --offload-arch=amdgcnspirv \ | ||
| // RUN: -nogpuinc -nogpulib -### -x hip %s -save-temps \ | ||
| // RUN: -use-spirv-backend -S \ | ||
| // RUN: 2>&1 | FileCheck %s --check-prefixes=CHECK-SPIRV-TRANSLATOR,CHECK-SPIRV-BACKEND-BC | ||
|
|
||
| // RUN: %clang --offload-new-driver --target=x86_64-unknown-linux-gnu --offload-arch=amdgcnspirv \ | ||
| // RUN: -nogpuinc -nogpulib -### -x hip %s -save-temps \ | ||
| // RUN: -use-spirv-backend -fgpu-rdc \ | ||
| // RUN: 2>&1 | FileCheck %s --check-prefixes=CHECK-SPIRV-TRANSLATOR,CHECK-SPIRV-BACKEND-BC,CHECK-CLANG-LINKER-WRAPPER | ||
|
|
||
| // RUN: %clang --offload-new-driver --target=x86_64-unknown-linux-gnu --offload-arch=amdgcnspirv \ | ||
| // RUN: -nogpuinc -nogpulib -### -x hip %s -save-temps \ | ||
| // RUN: -use-spirv-backend \ | ||
| // RUN: 2>&1 | FileCheck %s --check-prefixes=CHECK-SPIRV-TRANSLATOR,CHECK-SPIRV-BACKEND-BC,CHECK-CLANG-LINKER-WRAPPER | ||
|
|
||
| // CHECK-SPIRV-TRANSLATOR-NOT: "{{.*llvm-spirv.*}}" | ||
| // CHECK-SPIRV-BACKEND-TEXTUAL: "{{.*}}clang{{.*}}" "-cc1" "-triple" "spirv64-amd-amdhsa" {{.*}} "-S" | ||
| // CHECK-SPIRV-BACKEND-BINARY: "{{.*}}clang{{.*}}" "-cc1" "-triple" "spirv64-amd-amdhsa" {{.*}} "-emit-obj" | ||
| // CHECK-SPIRV-BACKEND-BC: "{{.*}}clang{{.*}}" "-cc1" "-triple" "spirv64-amd-amdhsa" {{.*}} "-emit-llvm-bc" | ||
| // CHECK-SPIRV-BACKEND-LL: "{{.*}}clang{{.*}}" "-cc1" "-triple" "spirv64-amd-amdhsa" {{.*}} "-emit-llvm" | ||
| // CHECK-FGPU-RDC-SAME: {{.*}} "-fgpu-rdc" | ||
| // CHECK-CLANG-LINKER-WRAPPER: "{{.*}}clang-linker-wrapper" "--should-extract=amdgcnspirv" {{.*}} "--device-compiler=spirv64-amd-amdhsa=-use-spirv-backend" |
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.