Fixes #153304 Changes: - When writing `ConstantExpr` GEPs to DXIL bitcode, the bitcode writer will use the old Constant Code `CST_CODE_CE_GEP_OLD = 12` instead of the newer `CST_CODE_CE_GEP = 32` which is interpreted as an undef in DXIL. Additional context: [CST_CODE_CE_GEP = 12 in DXC](0c9e75e7e9/include/llvm/Bitcode/LLVMBitCodes.h (L187)) while the same constant code is labeled [CST_CODE_CE_GEP_OLD in LLVM](65de318d18/llvm/include/llvm/Bitcode/LLVMBitCodes.h (L411)) - Modifies the `PointerTypeAnalysis` to be able to analyze pointer-typed constants that appear in the operands of instructions so that the correct type of the `ConstantExpr` GEP is determined and written into the DXIL bitcode. - Adds a `PointerTypeAnalysis` test and dxil-dis test to ensure that the pointer type of `ConstantExpr` GEPs are resolved and `ConstantExpr` GEPs are written to DXIL bitcode correctly In addition, this PR also adds a missing call to `GV.removeDeadConstantUsers()` in the DXILFinalizeLinkage pass, and removes an unnecessary manual removal of a ConstantExpr in the DXILFlattenArrays pass.
81 lines
2.3 KiB
C++
81 lines
2.3 KiB
C++
//===- 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) {
|
|
bool MadeChange = false;
|
|
|
|
// Convert private globals and external globals with no usage to internal
|
|
// linkage.
|
|
for (GlobalVariable &GV : M.globals()) {
|
|
GV.removeDeadConstantUsers();
|
|
if (GV.hasPrivateLinkage() || (GV.hasExternalLinkage() && GV.use_empty())) {
|
|
GV.setLinkage(GlobalValue::InternalLinkage);
|
|
MadeChange = true;
|
|
}
|
|
}
|
|
|
|
SmallVector<Function *> Funcs;
|
|
|
|
// Collect non-entry and non-exported functions to set to internal linkage.
|
|
for (Function &EF : M.functions()) {
|
|
if (EF.isIntrinsic())
|
|
continue;
|
|
if (EF.hasExternalLinkage() && EF.hasDefaultVisibility())
|
|
continue;
|
|
if (EF.hasFnAttribute("hlsl.shader"))
|
|
continue;
|
|
Funcs.push_back(&EF);
|
|
}
|
|
|
|
for (Function *F : Funcs) {
|
|
if (F->getLinkage() == GlobalValue::ExternalLinkage) {
|
|
F->setLinkage(GlobalValue::InternalLinkage);
|
|
MadeChange = true;
|
|
}
|
|
if (F->isDefTriviallyDead()) {
|
|
M.getFunctionList().erase(F);
|
|
MadeChange = true;
|
|
}
|
|
}
|
|
|
|
return MadeChange;
|
|
}
|
|
|
|
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();
|
|
}
|