[clang] [llvm] [HLSL] Create HLSL legalization passes (PR #123811)
Steven Perron via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 21 11:57:13 PST 2025
https://github.com/s-perron created https://github.com/llvm/llvm-project/pull/123811
The semantics of HLSL require passes some passes to be run that will
modify the llvm-ir created by clang to match what is expected by the
backend.
In this PR, wewe move the DXILFinalizeLinkage to be run for all backend.
The pass is also renamed to HLSLFinalizeLinkage. A pass of the global opt
pass is run afterwards.
Fixes #122767.
>From 2de3c0dd05e415bdf3e32ec0aaaec89183606314 Mon Sep 17 00:00:00 2001
From: Steven Perron <stevenperron at google.com>
Date: Thu, 16 Jan 2025 11:32:16 -0500
Subject: [PATCH] [HLSL] Create HLSL legalization passes
The semantics of HLSL require passes some passes to be run that will
modify the llvm-ir created by clang to match what is expected by the
backend.
In this PR, wewe move the DXILFinalizeLinkage to be run for all backend.
The pass is also renamed to HLSLFinalizeLinkage. A pass of the global opt
pass is run afterwards.
Fixes #122767.
---
clang/lib/CodeGen/BackendUtil.cpp | 8 +++
clang/lib/CodeGen/CMakeLists.txt | 1 +
clang/test/CodeGenHLSL/inline-functions.hlsl | 2 +-
llvm/lib/Target/DirectX/CMakeLists.txt | 1 -
.../Target/DirectX/DXILFinalizeLinkage.cpp | 62 -------------------
llvm/lib/Target/DirectX/DXILFinalizeLinkage.h | 39 ------------
.../Target/DirectX/DirectXTargetMachine.cpp | 2 -
llvm/lib/Transforms/CMakeLists.txt | 1 +
llvm/lib/Transforms/HLSL/CMakeLists.txt | 18 ++++++
9 files changed, 29 insertions(+), 105 deletions(-)
delete mode 100644 llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp
delete mode 100644 llvm/lib/Target/DirectX/DXILFinalizeLinkage.h
create mode 100644 llvm/lib/Transforms/HLSL/CMakeLists.txt
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index 3951ad01497cca..461e1fe176a6e0 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -57,6 +57,7 @@
#include "llvm/TargetParser/SubtargetFeature.h"
#include "llvm/TargetParser/Triple.h"
#include "llvm/Transforms/HipStdPar/HipStdPar.h"
+#include "llvm/Transforms/HLSL/HLSLFinalizeLinkage.h"
#include "llvm/Transforms/IPO/EmbedBitcodePass.h"
#include "llvm/Transforms/IPO/LowerTypeTests.h"
#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
@@ -82,6 +83,7 @@
#include "llvm/Transforms/ObjCARC.h"
#include "llvm/Transforms/Scalar/EarlyCSE.h"
#include "llvm/Transforms/Scalar/GVN.h"
+#include "llvm/Transforms/IPO/GlobalOpt.h"
#include "llvm/Transforms/Scalar/JumpThreading.h"
#include "llvm/Transforms/Utils/Debugify.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
@@ -1164,6 +1166,12 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
LangOpts.HIPStdParInterposeAlloc)
MPM.addPass(HipStdParAllocationInterpositionPass());
+ if (LangOpts.HLSL) {
+ // HLSL legalization passes
+ MPM.addPass(HLSLFinalizeLinkage());
+ MPM.addPass(GlobalOptPass());
+ }
+
// Now that we have all of the passes ready, run them.
{
PrettyStackTraceString CrashInfo("Optimizer");
diff --git a/clang/lib/CodeGen/CMakeLists.txt b/clang/lib/CodeGen/CMakeLists.txt
index 868ec847b9634b..1c7929219fcfcf 100644
--- a/clang/lib/CodeGen/CMakeLists.txt
+++ b/clang/lib/CodeGen/CMakeLists.txt
@@ -14,6 +14,7 @@ set(LLVM_LINK_COMPONENTS
FrontendOpenMP
FrontendOffloading
HIPStdPar
+ HLSL
IPO
IRPrinter
IRReader
diff --git a/clang/test/CodeGenHLSL/inline-functions.hlsl b/clang/test/CodeGenHLSL/inline-functions.hlsl
index fa9c88db26dfc2..37bd2535a5c37d 100644
--- a/clang/test/CodeGenHLSL/inline-functions.hlsl
+++ b/clang/test/CodeGenHLSL/inline-functions.hlsl
@@ -61,7 +61,7 @@ unsigned RemoveDupes(unsigned Buf[MAX], unsigned size) {
}
-RWBuffer<unsigned> Indices;
+RWBuffer<unsigned> Indices : register(u0);
// The mangled version of main only remains without inlining
// because it has internal linkage from the start
diff --git a/llvm/lib/Target/DirectX/CMakeLists.txt b/llvm/lib/Target/DirectX/CMakeLists.txt
index 26315db891b577..b7d2034fbdadb7 100644
--- a/llvm/lib/Target/DirectX/CMakeLists.txt
+++ b/llvm/lib/Target/DirectX/CMakeLists.txt
@@ -21,7 +21,6 @@ add_llvm_target(DirectXCodeGen
DirectXTargetTransformInfo.cpp
DXContainerGlobals.cpp
DXILDataScalarization.cpp
- DXILFinalizeLinkage.cpp
DXILFlattenArrays.cpp
DXILIntrinsicExpansion.cpp
DXILOpBuilder.cpp
diff --git a/llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp b/llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp
deleted file mode 100644
index 91ac758150fb4c..00000000000000
--- a/llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp
+++ /dev/null
@@ -1,62 +0,0 @@
-//===- 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> Funcs;
-
- // Collect non-entry and non-exported functions to set to internal linkage.
- for (Function &EF : M.functions()) {
- if (EF.isIntrinsic())
- continue;
- if (EF.hasFnAttribute("hlsl.shader") || EF.hasFnAttribute("hlsl.export"))
- continue;
- Funcs.insert(&EF);
- }
-
- for (Function *F : Funcs) {
- if (F->getLinkage() == GlobalValue::ExternalLinkage)
- F->setLinkage(GlobalValue::InternalLinkage);
- if (F->isDefTriviallyDead())
- M.getFunctionList().erase(F);
- }
-
- 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();
-}
diff --git a/llvm/lib/Target/DirectX/DXILFinalizeLinkage.h b/llvm/lib/Target/DirectX/DXILFinalizeLinkage.h
deleted file mode 100644
index aab1bc3f7a28e2..00000000000000
--- a/llvm/lib/Target/DirectX/DXILFinalizeLinkage.h
+++ /dev/null
@@ -1,39 +0,0 @@
-//===- 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
diff --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
index ecb1bf775f8578..c969abb43f858b 100644
--- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
+++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
@@ -61,7 +61,6 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeDirectXTarget() {
initializeDXILTranslateMetadataLegacyPass(*PR);
initializeDXILResourceMDWrapperPass(*PR);
initializeShaderFlagsAnalysisWrapperPass(*PR);
- initializeDXILFinalizeLinkageLegacyPass(*PR);
}
class DXILTargetObjectFile : public TargetLoweringObjectFile {
@@ -91,7 +90,6 @@ class DirectXPassConfig : public TargetPassConfig {
FunctionPass *createTargetRegisterAllocator(bool) override { return nullptr; }
void addCodeGenPrepare() override {
- addPass(createDXILFinalizeLinkageLegacyPass());
addPass(createDXILIntrinsicExpansionLegacyPass());
addPass(createDXILDataScalarizationLegacyPass());
addPass(createDXILFlattenArraysLegacyPass());
diff --git a/llvm/lib/Transforms/CMakeLists.txt b/llvm/lib/Transforms/CMakeLists.txt
index 7046f2f4b1d2c1..e3c13a19f6de17 100644
--- a/llvm/lib/Transforms/CMakeLists.txt
+++ b/llvm/lib/Transforms/CMakeLists.txt
@@ -9,3 +9,4 @@ add_subdirectory(ObjCARC)
add_subdirectory(Coroutines)
add_subdirectory(CFGuard)
add_subdirectory(HipStdPar)
+add_subdirectory(HLSL)
diff --git a/llvm/lib/Transforms/HLSL/CMakeLists.txt b/llvm/lib/Transforms/HLSL/CMakeLists.txt
new file mode 100644
index 00000000000000..df6af5a6469cbd
--- /dev/null
+++ b/llvm/lib/Transforms/HLSL/CMakeLists.txt
@@ -0,0 +1,18 @@
+add_llvm_component_library(LLVMHlsl
+ HLSLFinalizeLinkage.cpp
+
+ ADDITIONAL_HEADER_DIRS
+ ${LLVM_MAIN_INCLUDE_DIR}/llvm/Transforms/HLSL
+
+ DEPENDS
+ intrinsics_gen
+ LLVMAnalysis
+
+ COMPONENT_NAME
+ HLSL
+
+ LINK_COMPONENTS
+ Analysis
+ Core
+ Support
+ TransformUtils)
More information about the llvm-commits
mailing list