Skip to content

Commit 834f6be

Browse files
committed
Only run new pass if llvm passes are not disabled.
1 parent 4e62f38 commit 834f6be

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
10991099
if (CodeGenOpts.LinkBitcodePostopt)
11001100
MPM.addPass(LinkInModulesPass(BC));
11011101

1102-
if (LangOpts.HLSL) {
1102+
if (LangOpts.HLSL && !CodeGenOpts.DisableLLVMPasses) {
11031103
// HLSL legalization passes
11041104
MPM.addPass(HLSLFinalizeLinkage());
11051105
MPM.addPass(GlobalOptPass());
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===- HLSLFinalizeLinkage.h - Finalize linkage of functions --------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// HLSLFinalizeLinkage pass updates the linkage of functions to make sure only
10+
/// shader entry points and exported functions are visible from the module (have
11+
/// program linkage). All other functions and variables will be updated to have
12+
/// internal linkage.
13+
///
14+
//===----------------------------------------------------------------------===//
15+
16+
#ifndef LLVM_TRANSFORMS_HLSL_HLSLFINALIZELINKAGE_H
17+
#define LLVM_TRANSFORMS_HLSL_HLSLFINALIZELINKAGE_H
18+
19+
#include "llvm/IR/PassManager.h"
20+
#include "llvm/Pass.h"
21+
22+
namespace llvm {
23+
24+
class HLSLFinalizeLinkage : public PassInfoMixin<HLSLFinalizeLinkage> {
25+
public:
26+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
27+
static bool isRequired() { return true; }
28+
};
29+
30+
class HLSLFinalizeLinkageLegacy : public ModulePass {
31+
public:
32+
HLSLFinalizeLinkageLegacy() : ModulePass(ID) {}
33+
bool runOnModule(Module &M) override;
34+
35+
static char ID; // Pass identification.
36+
};
37+
} // namespace llvm
38+
39+
#endif // LLVM_TRANSFORMS_HLSL_HLSLFINALIZELINKAGE_H
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===- HLSLFinalizeLinkage.cpp - Finalize linkage of functions ------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Transforms/HLSL/HLSLFinalizeLinkage.h"
10+
#include "llvm/IR/Function.h"
11+
#include "llvm/IR/GlobalValue.h"
12+
#include "llvm/IR/Module.h"
13+
14+
#define DEBUG_TYPE "dxil-finalize-linkage"
15+
16+
using namespace llvm;
17+
18+
static bool finalizeLinkage(Module &M) {
19+
SmallPtrSet<Function *, 8> Funcs;
20+
21+
for(auto &Var : M.globals()) {
22+
if (Var.getLinkage() == GlobalValue::ExternalLinkage) {
23+
Var.setLinkage(GlobalValue::InternalLinkage);
24+
}
25+
}
26+
27+
// Collect non-entry and non-exported functions to set to internal linkage.
28+
for (Function &EF : M.functions()) {
29+
if (EF.isIntrinsic())
30+
continue;
31+
if (EF.hasFnAttribute("hlsl.shader") || EF.hasFnAttribute("hlsl.export"))
32+
continue;
33+
Funcs.insert(&EF);
34+
}
35+
36+
for (Function *F : Funcs) {
37+
if (F->getLinkage() == GlobalValue::ExternalLinkage)
38+
F->setLinkage(GlobalValue::InternalLinkage);
39+
if (F->isDefTriviallyDead())
40+
M.getFunctionList().erase(F);
41+
}
42+
43+
return false;
44+
}
45+
46+
PreservedAnalyses HLSLFinalizeLinkage::run(Module &M,
47+
ModuleAnalysisManager &AM) {
48+
if (finalizeLinkage(M))
49+
return PreservedAnalyses::none();
50+
return PreservedAnalyses::all();
51+
}

0 commit comments

Comments
 (0)