Skip to content

[HLSL] Create HLSL legalization passes #123811

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CodeGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set(LLVM_LINK_COMPONENTS
FrontendOpenMP
FrontendOffloading
HIPStdPar
HLSL
IPO
IRPrinter
IRReader
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGenHLSL/inline-functions.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ unsigned RemoveDupes(unsigned Buf[MAX], unsigned size) {
}


RWBuffer<unsigned> Indices;
RWBuffer<unsigned> Indices : register(u0);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resources without explicit register assignments are not supported yet. If we do not do this then we end up with a handle that is zero initialized, which causes problems.


// The mangled version of main only remains without inlining
// because it has internal linkage from the start
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
//===- 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.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// 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<DXILFinalizeLinkage> {
class HLSLFinalizeLinkage : public PassInfoMixin<HLSLFinalizeLinkage> {
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
1 change: 0 additions & 1 deletion llvm/lib/Target/DirectX/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ add_llvm_target(DirectXCodeGen
DirectXTargetTransformInfo.cpp
DXContainerGlobals.cpp
DXILDataScalarization.cpp
DXILFinalizeLinkage.cpp
DXILFlattenArrays.cpp
DXILIntrinsicExpansion.cpp
DXILOpBuilder.cpp
Expand Down
2 changes: 0 additions & 2 deletions llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeDirectXTarget() {
initializeDXILTranslateMetadataLegacyPass(*PR);
initializeDXILResourceMDWrapperPass(*PR);
initializeShaderFlagsAnalysisWrapperPass(*PR);
initializeDXILFinalizeLinkageLegacyPass(*PR);
}

class DXILTargetObjectFile : public TargetLoweringObjectFile {
Expand Down Expand Up @@ -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());
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ add_subdirectory(ObjCARC)
add_subdirectory(Coroutines)
add_subdirectory(CFGuard)
add_subdirectory(HipStdPar)
add_subdirectory(HLSL)
18 changes: 18 additions & 0 deletions llvm/lib/Transforms/HLSL/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
//===- 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.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#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"
Expand All @@ -20,6 +18,12 @@ using namespace llvm;
static bool finalizeLinkage(Module &M) {
SmallPtrSet<Function *, 8> 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())
Expand All @@ -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();
}
Loading