-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HLSL] Change default linkage of HLSL functions to internal (#95331)
An HLSL function has internal linkage by default unless it is: 1. shader entry point function 2. marked with the `export` keyword (#92812) 3. patch constant function (not implemented yet) This PR adds a link-time pass `DXILFinalizeLinkage` that updates the linkage of functions to make sure only shader entry points and exported functions are visible from the module (have _program linkage_). All other functions will be updated to have internal linkage. Related spec update: microsoft/hlsl-specs#295 Fixes ##92071
- Loading branch information
Showing
8 changed files
with
178 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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,60 @@ | ||
//===- DXILFinalizeLinkage.cpp - Finalize linkage of functions ------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "DXILFinalizeLinkage.h" | ||
#include "DirectX.h" | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/IR/GlobalValue.h" | ||
#include "llvm/IR/Metadata.h" | ||
#include "llvm/IR/Module.h" | ||
|
||
#define DEBUG_TYPE "dxil-finalize-linkage" | ||
|
||
using namespace llvm; | ||
|
||
static bool finalizeLinkage(Module &M) { | ||
SmallPtrSet<Function *, 8> EntriesAndExports; | ||
|
||
// Find all entry points and export functions | ||
for (Function &EF : M.functions()) { | ||
if (!EF.hasFnAttribute("hlsl.shader") && !EF.hasFnAttribute("hlsl.export")) | ||
continue; | ||
EntriesAndExports.insert(&EF); | ||
} | ||
|
||
for (Function &F : M.functions()) { | ||
if (F.getLinkage() == GlobalValue::ExternalLinkage && | ||
!EntriesAndExports.contains(&F)) { | ||
F.setLinkage(GlobalValue::InternalLinkage); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
PreservedAnalyses DXILFinalizeLinkage::run(Module &M, | ||
ModuleAnalysisManager &AM) { | ||
if (finalizeLinkage(M)) | ||
return PreservedAnalyses::none(); | ||
return PreservedAnalyses::all(); | ||
} | ||
|
||
bool DXILFinalizeLinkageLegacy::runOnModule(Module &M) { | ||
return finalizeLinkage(M); | ||
} | ||
|
||
char DXILFinalizeLinkageLegacy::ID = 0; | ||
|
||
INITIALIZE_PASS_BEGIN(DXILFinalizeLinkageLegacy, DEBUG_TYPE, | ||
"DXIL Finalize Linkage", false, false) | ||
INITIALIZE_PASS_END(DXILFinalizeLinkageLegacy, DEBUG_TYPE, | ||
"DXIL Finalize Linkage", false, false) | ||
|
||
ModulePass *llvm::createDXILFinalizeLinkageLegacyPass() { | ||
return new DXILFinalizeLinkageLegacy(); | ||
} |
This file contains 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,39 @@ | ||
//===- DXILFinalizeLinkage.h - Finalize linkage of functions --------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// DXILFinalizeLinkage pass updates the linkage of functions to make sure only | ||
/// shader entry points and exported functions are visible from the module (have | ||
/// program linkage). All other functions will be updated to have internal | ||
/// linkage. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_TARGET_DIRECTX_DXILFINALIZELINKAGE_H | ||
#define LLVM_TARGET_DIRECTX_DXILFINALIZELINKAGE_H | ||
|
||
#include "llvm/IR/PassManager.h" | ||
#include "llvm/Pass.h" | ||
|
||
namespace llvm { | ||
|
||
class DXILFinalizeLinkage : public PassInfoMixin<DXILFinalizeLinkage> { | ||
public: | ||
PreservedAnalyses run(Module &M, ModuleAnalysisManager &); | ||
static bool isRequired() { return true; } | ||
}; | ||
|
||
class DXILFinalizeLinkageLegacy : public ModulePass { | ||
public: | ||
DXILFinalizeLinkageLegacy() : ModulePass(ID) {} | ||
bool runOnModule(Module &M) override; | ||
|
||
static char ID; // Pass identification. | ||
}; | ||
} // namespace llvm | ||
|
||
#endif // LLVM_TARGET_DIRECTX_DXILFINALIZELINKAGE_H |
This file contains 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 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 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 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,64 @@ | ||
; RUN: opt -S -dxil-finalize-linkage -mtriple=dxil-unknown-shadermodel6.5-compute %s | FileCheck %s | ||
; RUN: llc %s --filetype=asm -o - | FileCheck %s --check-prefixes=CHECK-LLC | ||
|
||
target triple = "dxilv1.5-pc-shadermodel6.5-compute" | ||
|
||
; DXILFinalizeLinkage changes linkage of all functions that are not | ||
; entry points or exported function to internal. | ||
|
||
; CHECK: define internal void @"?f1@@YAXXZ"() | ||
define void @"?f1@@YAXXZ"() #0 { | ||
entry: | ||
ret void | ||
} | ||
|
||
; CHECK: define internal void @"?f2@@YAXXZ"() | ||
define void @"?f2@@YAXXZ"() #0 { | ||
entry: | ||
ret void | ||
} | ||
|
||
; CHECK: define internal void @"?f3@@YAXXZ"() | ||
define void @"?f3@@YAXXZ"() #0 { | ||
entry: | ||
ret void | ||
} | ||
|
||
; CHECK: define internal void @"?foo@@YAXXZ"() | ||
define void @"?foo@@YAXXZ"() #0 { | ||
entry: | ||
call void @"?f2@@YAXXZ"() #3 | ||
ret void | ||
} | ||
|
||
; Exported function - do not change linkage | ||
; CHECK: define void @"?bar@@YAXXZ"() | ||
define void @"?bar@@YAXXZ"() #1 { | ||
entry: | ||
call void @"?f3@@YAXXZ"() #3 | ||
ret void | ||
} | ||
|
||
; CHECK: define internal void @"?main@@YAXXZ"() #0 | ||
define internal void @"?main@@YAXXZ"() #0 { | ||
entry: | ||
call void @"?foo@@YAXXZ"() #3 | ||
call void @"?bar@@YAXXZ"() #3 | ||
ret void | ||
} | ||
|
||
; Entry point function - do not change linkage | ||
; CHECK: define void @main() #2 | ||
define void @main() #2 { | ||
entry: | ||
call void @"?main@@YAXXZ"() | ||
ret void | ||
} | ||
|
||
attributes #0 = { convergent noinline nounwind optnone} | ||
attributes #1 = { convergent noinline nounwind optnone "hlsl.export"} | ||
attributes #2 = { convergent "hlsl.numthreads"="4,1,1" "hlsl.shader"="compute"} | ||
attributes #3 = { convergent } | ||
|
||
; Make sure "hlsl.export" attribute is stripped by llc | ||
; CHECK-LLC-NOT: "hlsl.export" |
This file contains 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