diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 3951ad01497cc..dc71ea4c346fd 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -56,8 +56,10 @@ #include "llvm/Target/TargetOptions.h" #include "llvm/TargetParser/SubtargetFeature.h" #include "llvm/TargetParser/Triple.h" +#include "llvm/Transforms/HLSL/HLSLFinalizeLinkage.h" #include "llvm/Transforms/HipStdPar/HipStdPar.h" #include "llvm/Transforms/IPO/EmbedBitcodePass.h" +#include "llvm/Transforms/IPO/GlobalOpt.h" #include "llvm/Transforms/IPO/LowerTypeTests.h" #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h" #include "llvm/Transforms/InstCombine/InstCombine.h" @@ -1097,6 +1099,12 @@ void EmitAssemblyHelper::RunOptimizationPipeline( if (CodeGenOpts.LinkBitcodePostopt) MPM.addPass(LinkInModulesPass(BC)); + if (LangOpts.HLSL && !CodeGenOpts.DisableLLVMPasses) { + // HLSL legalization passes + MPM.addPass(HLSLFinalizeLinkage()); + MPM.addPass(GlobalOptPass()); + } + // Add a verifier pass if requested. We don't have to do this if the action // requires code generation because there will already be a verifier pass in // the code-generation pipeline. diff --git a/clang/lib/CodeGen/CMakeLists.txt b/clang/lib/CodeGen/CMakeLists.txt index 868ec847b9634..1c7929219fcfc 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 fa9c88db26dfc..37bd2535a5c37 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 Indices; +RWBuffer 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/DXILFinalizeLinkage.h b/llvm/include/llvm/Transforms/HLSL/HLSLFinalizeLinkage.h similarity index 59% rename from llvm/lib/Target/DirectX/DXILFinalizeLinkage.h rename to llvm/include/llvm/Transforms/HLSL/HLSLFinalizeLinkage.h index aab1bc3f7a28e..2da4a41673ef0 100644 --- a/llvm/lib/Target/DirectX/DXILFinalizeLinkage.h +++ b/llvm/include/llvm/Transforms/HLSL/HLSLFinalizeLinkage.h @@ -1,4 +1,4 @@ -//===- DXILFinalizeLinkage.h - Finalize linkage of functions --------------===// +//===- HLSLFinalizeLinkage.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. @@ -6,34 +6,34 @@ // //===----------------------------------------------------------------------===// /// -/// DXILFinalizeLinkage pass updates the linkage of functions to make sure only +/// HLSLFinalizeLinkage 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. +/// program linkage). All other functions and variables will be updated to have +/// internal linkage. /// //===----------------------------------------------------------------------===// -#ifndef LLVM_TARGET_DIRECTX_DXILFINALIZELINKAGE_H -#define LLVM_TARGET_DIRECTX_DXILFINALIZELINKAGE_H +#ifndef LLVM_TRANSFORMS_HLSL_HLSLFINALIZELINKAGE_H +#define LLVM_TRANSFORMS_HLSL_HLSLFINALIZELINKAGE_H #include "llvm/IR/PassManager.h" #include "llvm/Pass.h" namespace llvm { -class DXILFinalizeLinkage : public PassInfoMixin { +class HLSLFinalizeLinkage : public PassInfoMixin { public: PreservedAnalyses run(Module &M, ModuleAnalysisManager &); static bool isRequired() { return true; } }; -class DXILFinalizeLinkageLegacy : public ModulePass { +class HLSLFinalizeLinkageLegacy : public ModulePass { public: - DXILFinalizeLinkageLegacy() : ModulePass(ID) {} + HLSLFinalizeLinkageLegacy() : ModulePass(ID) {} bool runOnModule(Module &M) override; static char ID; // Pass identification. }; } // namespace llvm -#endif // LLVM_TARGET_DIRECTX_DXILFINALIZELINKAGE_H +#endif // LLVM_TRANSFORMS_HLSL_HLSLFINALIZELINKAGE_H diff --git a/llvm/lib/Target/DirectX/CMakeLists.txt b/llvm/lib/Target/DirectX/CMakeLists.txt index 26315db891b57..b7d2034fbdadb 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/DirectXTargetMachine.cpp b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp index ecb1bf775f857..c969abb43f858 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 7046f2f4b1d2c..e3c13a19f6de1 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 0000000000000..df6af5a6469cb --- /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) diff --git a/llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp b/llvm/lib/Transforms/HLSL/HLSLFinalizeLinkage.cpp similarity index 64% rename from llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp rename to llvm/lib/Transforms/HLSL/HLSLFinalizeLinkage.cpp index 91ac758150fb4..8b0b6ea1c1dd6 100644 --- a/llvm/lib/Target/DirectX/DXILFinalizeLinkage.cpp +++ b/llvm/lib/Transforms/HLSL/HLSLFinalizeLinkage.cpp @@ -1,4 +1,4 @@ -//===- DXILFinalizeLinkage.cpp - Finalize linkage of functions ------------===// +//===- HLSLFinalizeLinkage.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. @@ -6,11 +6,9 @@ // //===----------------------------------------------------------------------===// -#include "DXILFinalizeLinkage.h" -#include "DirectX.h" +#include "llvm/Transforms/HLSL/HLSLFinalizeLinkage.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" @@ -20,6 +18,12 @@ using namespace llvm; static bool finalizeLinkage(Module &M) { SmallPtrSet Funcs; + for(auto &Var : M.globals()) { + if (Var.getLinkage() == GlobalValue::ExternalLinkage) { + Var.setLinkage(GlobalValue::InternalLinkage); + } + } + // Collect non-entry and non-exported functions to set to internal linkage. for (Function &EF : M.functions()) { if (EF.isIntrinsic()) @@ -39,24 +43,9 @@ static bool finalizeLinkage(Module &M) { return false; } -PreservedAnalyses DXILFinalizeLinkage::run(Module &M, +PreservedAnalyses HLSLFinalizeLinkage::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(); -}