-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[SPIRV][SPIRVPrepareGlobals] Convert llvm.embedded.module from a 0-element array to a 1-element array #166950
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
jmmartinez
merged 5 commits into
main
from
users/jmmartinez/spirv/amd_embed_bitcode_module
Nov 12, 2025
+131
−0
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e43cefc
[SPIRV][SPIRVPrepareGlobals] Add pass skeleton
jmmartinez eb7f340
Pre-commit test: [SPIRV][SPIRVPrepareGlobals] tryExtendLLVMBitcodeMarker
jmmartinez 5815524
[SPIRV][SPIRVPrepareGlobals] Convert llvm.embedded.module from a 0-el…
jmmartinez 4d9159a
Add missing . to comment
jmmartinez 7cb1fc5
Add missing . to comment and embeded->embedded
jmmartinez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| //===-- SPIRVPrepareGlobals.cpp - Prepare IR SPIRV globals ------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // The pass transforms IR globals that cannot be trivially mapped to SPIRV | ||
| // into something that is trival to lower. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "SPIRV.h" | ||
|
|
||
| #include "llvm/IR/Module.h" | ||
|
|
||
| using namespace llvm; | ||
|
|
||
| namespace { | ||
|
|
||
| struct SPIRVPrepareGlobals : public ModulePass { | ||
| static char ID; | ||
| SPIRVPrepareGlobals() : ModulePass(ID) {} | ||
|
|
||
| StringRef getPassName() const override { | ||
| return "SPIRV prepare global variables"; | ||
| } | ||
|
|
||
| bool runOnModule(Module &M) override; | ||
| }; | ||
|
|
||
| bool tryExtendLLVMBitcodeMarker(GlobalVariable &Bitcode) { | ||
| assert(Bitcode.getName() == "llvm.embedded.module"); | ||
|
|
||
| ArrayType *AT = cast<ArrayType>(Bitcode.getValueType()); | ||
| if (AT->getNumElements() != 0) | ||
| return false; | ||
|
|
||
| ArrayType *AT1 = ArrayType::get(AT->getElementType(), 1); | ||
| Constant *OneEltInit = Constant::getNullValue(AT1); | ||
| Bitcode.replaceInitializer(OneEltInit); | ||
| return true; | ||
| } | ||
|
|
||
| bool SPIRVPrepareGlobals::runOnModule(Module &M) { | ||
| const bool IsAMD = M.getTargetTriple().getVendor() == Triple::AMD; | ||
| if (!IsAMD) | ||
| return false; | ||
|
|
||
| bool Changed = false; | ||
| if (GlobalVariable *Bitcode = M.getNamedGlobal("llvm.embedded.module")) | ||
| Changed |= tryExtendLLVMBitcodeMarker(*Bitcode); | ||
|
|
||
| return Changed; | ||
| } | ||
| char SPIRVPrepareGlobals::ID = 0; | ||
|
|
||
| } // namespace | ||
|
|
||
| INITIALIZE_PASS(SPIRVPrepareGlobals, "prepare-globals", | ||
| "SPIRV prepare global variables", false, false) | ||
|
|
||
| namespace llvm { | ||
| ModulePass *createSPIRVPrepareGlobalsPass() { | ||
| return new SPIRVPrepareGlobals(); | ||
| } | ||
| } // namespace llvm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| ; Expanding the bitcode marker works only for AMD at the moment | ||
| ; RUN: not llc -verify-machineinstrs -mtriple=spirv-unknown-unknown %s -o - | ||
| ; RUN: llc -verify-machineinstrs -mtriple=spirv64-amd-amdhsa %s -o - | FileCheck %s | ||
| ; RUN: %if spirv-tools %{ llc -mtriple=spirv64-amd-amdhsa %s -o - -filetype=obj | spirv-val %} | ||
| ; | ||
| ; Verify that we lower the embedded bitcode | ||
jmmartinez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @llvm.embedded.module = private addrspace(1) constant [0 x i8] zeroinitializer, section ".llvmbc", align 1 | ||
| @llvm.compiler.used = appending addrspace(1) global [1 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @llvm.embedded.module to ptr addrspace(4))], section "llvm.metadata" | ||
|
|
||
| ; CHECK: OpName %[[#LLVM_EMBEDDED_MODULE:]] "llvm.embedded.module" | ||
| ; CHECK: OpDecorate %[[#LLVM_EMBEDDED_MODULE]] Constant | ||
| ; CHECK: %[[#UCHAR:]] = OpTypeInt 8 0 | ||
| ; CHECK: %[[#UINT:]] = OpTypeInt 32 0 | ||
| ; CHECK: %[[#ONE:]] = OpConstant %[[#UINT]] 1 | ||
| ; CHECK: %[[#UCHAR_ARR_1:]] = OpTypeArray %[[#UCHAR]] %[[#ONE]] | ||
| ; CHECK: %[[#UCHAR_ARR_1_PTR:]] = OpTypePointer CrossWorkgroup %[[#UCHAR_ARR_1]] | ||
| ; CHECK: %[[#CONST_UCHAR_ARR_1:]] = OpConstantNull %[[#UCHAR_ARR_1]] | ||
| ; CHECK: %[[#LLVM_EMBEDDED_MODULE]] = OpVariable %[[#UCHAR_ARR_1_PTR]] CrossWorkgroup %[[#CONST_UCHAR_ARR_1]] | ||
|
|
||
| define spir_kernel void @foo() { | ||
| entry: | ||
| ret void | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| ; RUN: llc -verify-machineinstrs -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s | ||
| ; RUN: %if spirv-tools %{ llc -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %} | ||
| ; RUN: llc -verify-machineinstrs -mtriple=spirv64-amd-amdhsa %s -o - | FileCheck %s | ||
| ; RUN: %if spirv-tools %{ llc -mtriple=spirv64-amd-amdhsa %s -o - -filetype=obj | spirv-val %} | ||
| ; | ||
| ; Verify that we can lower the embeded module and cmdline | ||
jmmartinez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @llvm.embedded.module = private addrspace(1) constant [4 x i8] c"BC\C0\DE", section ".llvmbc", align 1 | ||
| @llvm.cmdline = private addrspace(1) constant [5 x i8] c"-cc1\00", section ".llvmcmd", align 1 | ||
| @llvm.compiler.used = appending addrspace(1) global [2 x ptr addrspace(4)] [ptr addrspace(4) addrspacecast (ptr addrspace(1) @llvm.embedded.module to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @llvm.cmdline to ptr addrspace(4))], section "llvm.metadata" | ||
|
|
||
| ; CHECK: OpName %[[#LLVM_EMBEDDED_MODULE:]] "llvm.embedded.module" | ||
| ; CHECK: OpName %[[#LLVM_CMDLINE:]] "llvm.cmdline" | ||
| ; CHECK: OpDecorate %[[#LLVM_EMBEDDED_MODULE]] Constant | ||
| ; CHECK: OpDecorate %[[#LLVM_CMDLINE]] Constant | ||
| ; CHECK: %[[#UCHAR:]] = OpTypeInt 8 0 | ||
| ; CHECK: %[[#UINT:]] = OpTypeInt 32 0 | ||
| ; CHECK: %[[#FIVE:]] = OpConstant %[[#UINT]] 5 | ||
| ; CHECK: %[[#UCHAR_ARR_5:]] = OpTypeArray %[[#UCHAR]] %[[#FIVE]] | ||
| ; CHECK: %[[#FOUR:]] = OpConstant %[[#UINT]] 4 | ||
| ; CHECK: %[[#UCHAR_ARR_4:]] = OpTypeArray %[[#UCHAR]] %[[#FOUR]] | ||
| ; CHECK: %[[#UCHAR_ARR_5_PTR:]] = OpTypePointer CrossWorkgroup %[[#UCHAR_ARR_5]] | ||
| ; CHECK: %[[#UCHAR_ARR_4_PTR:]] = OpTypePointer CrossWorkgroup %[[#UCHAR_ARR_4]] | ||
| ; CHECK: %[[#CONST_UCHAR_ARR_4:]] = OpConstantComposite %[[#UCHAR_ARR_4]] | ||
| ; CHECK: %[[#LLVM_EMBEDDED_MODULE]] = OpVariable %[[#UCHAR_ARR_4_PTR]] CrossWorkgroup %[[#CONST_UCHAR_ARR_4]] | ||
| ; CHECK: %[[#CONST_UCHAR_ARR_5:]] = OpConstantComposite %[[#UCHAR_ARR_5]] | ||
| ; CHECK: %[[#LLVM_CMDLINE]] = OpVariable %[[#UCHAR_ARR_5_PTR]] CrossWorkgroup %[[#CONST_UCHAR_ARR_5]] | ||
|
|
||
| define spir_kernel void @foo() { | ||
| entry: | ||
| ret void | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.