Skip to content
Open
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
6 changes: 6 additions & 0 deletions clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Analysis/GlobalsModRef.h"
#include "llvm/Analysis/RuntimeLibcallInfo.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Bitcode/BitcodeReader.h"
Expand Down Expand Up @@ -667,6 +668,11 @@ bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
llvm::driver::createTLII(TargetTriple, CodeGenOpts.getVecLib()));
CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));

const llvm::TargetOptions &Options = TM->Options;
CodeGenPasses.add(new RuntimeLibraryInfoWrapper(
TargetTriple, Options.ExceptionModel, Options.FloatABIType,
Options.EABIVersion, Options.MCOptions.ABIName, Options.VecLib));

// Normal mode, emit a .s or .o file by running the code generator. Note,
// this also adds codegenerator level optimization passes.
CodeGenFileType CGFT = getCodeGenFileType(Action);
Expand Down
15 changes: 12 additions & 3 deletions llvm/include/llvm/Analysis/RuntimeLibcallInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ class LLVM_ABI RuntimeLibraryAnalysis
RuntimeLibraryAnalysis() = default;
RuntimeLibraryAnalysis(RTLIB::RuntimeLibcallsInfo &&BaselineInfoImpl)
: LibcallsInfo(std::move(BaselineInfoImpl)) {}
explicit RuntimeLibraryAnalysis(const Triple &T) : LibcallsInfo(T) {}
RuntimeLibraryAnalysis(
const Triple &TT,
ExceptionHandling ExceptionModel = ExceptionHandling::None,
FloatABI::ABIType FloatABI = FloatABI::Default,
EABI EABIVersion = EABI::Default, StringRef ABIName = "",
VectorLibrary VecLib = VectorLibrary::NoLibrary);

LLVM_ABI RTLIB::RuntimeLibcallsInfo run(const Module &M,
ModuleAnalysisManager &);
Expand All @@ -41,8 +46,12 @@ class LLVM_ABI RuntimeLibraryInfoWrapper : public ImmutablePass {
public:
static char ID;
RuntimeLibraryInfoWrapper();
explicit RuntimeLibraryInfoWrapper(const Triple &T);
explicit RuntimeLibraryInfoWrapper(const RTLIB::RuntimeLibcallsInfo &RTLCI);
RuntimeLibraryInfoWrapper(
const Triple &TT,
ExceptionHandling ExceptionModel = ExceptionHandling::None,
FloatABI::ABIType FloatABI = FloatABI::Default,
EABI EABIVersion = EABI::Default, StringRef ABIName = "",
VectorLibrary VecLib = VectorLibrary::NoLibrary);
Copy link
Contributor

Choose a reason for hiding this comment

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

Are the default values required? AFAICS they are always explicitly specified


const RTLIB::RuntimeLibcallsInfo &getRTLCI(const Module &M) {
ModuleAnalysisManager DummyMAM;
Expand Down
68 changes: 68 additions & 0 deletions llvm/include/llvm/CodeGen/LibcallLoweringInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
#ifndef LLVM_CODEGEN_LIBCALLLOWERINGINFO_H
#define LLVM_CODEGEN_LIBCALLLOWERINGINFO_H

#include "llvm/ADT/DenseMap.h"
#include "llvm/IR/RuntimeLibcalls.h"
#include "llvm/Pass.h"

namespace llvm {

class TargetSubtargetInfo;
class TargetMachine;

/// Tracks which library functions to use for a particular subtarget.
class LibcallLoweringInfo {
private:
const RTLIB::RuntimeLibcallsInfo &RTLCI;
Expand Down Expand Up @@ -73,6 +77,70 @@ class LibcallLoweringInfo {
}
};

/// Record a mapping from subtarget to LibcallLoweringInfo.
class LibcallLoweringModuleAnalysisResult {
private:
using LibcallLoweringMap =
DenseMap<const TargetSubtargetInfo *, LibcallLoweringInfo>;
mutable LibcallLoweringMap LoweringMap;
const RTLIB::RuntimeLibcallsInfo *RTLCI = nullptr;

public:
LibcallLoweringModuleAnalysisResult() = default;
LibcallLoweringModuleAnalysisResult(RTLIB::RuntimeLibcallsInfo &RTLCI)
: RTLCI(&RTLCI) {}

void init(const RTLIB::RuntimeLibcallsInfo *RT) { RTLCI = RT; }

void clear() {
RTLCI = nullptr;
LoweringMap.clear();
}

LLVM_ABI bool invalidate(Module &, const PreservedAnalyses &,
ModuleAnalysisManager::Invalidator &);

const LibcallLoweringInfo &
getLibcallLowering(const TargetSubtargetInfo &Subtarget) const {
return LoweringMap.try_emplace(&Subtarget, *RTLCI, Subtarget).first->second;
}
};

class LibcallLoweringModuleAnalysis
: public AnalysisInfoMixin<LibcallLoweringModuleAnalysis> {
private:
friend AnalysisInfoMixin<LibcallLoweringModuleAnalysis>;
LLVM_ABI static AnalysisKey Key;
Copy link
Contributor

Choose a reason for hiding this comment

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

private members shouldn't be LLVM_ABI


LibcallLoweringModuleAnalysisResult LibcallLoweringMap;

public:
using Result = LibcallLoweringModuleAnalysisResult;

LLVM_ABI Result run(Module &M, ModuleAnalysisManager &);
};

class LLVM_ABI LibcallLoweringInfoWrapper : public ImmutablePass {
LibcallLoweringModuleAnalysisResult Result;

public:
static char ID;
LibcallLoweringInfoWrapper();

const LibcallLoweringInfo &
getLibcallLowering(const TargetSubtargetInfo &Subtarget) const {
return Result.getLibcallLowering(Subtarget);
}

const LibcallLoweringModuleAnalysisResult &getResult() const {
return Result;
}

bool doInitialization(Module &M) override;
void getAnalysisUsage(AnalysisUsage &AU) const override;
void releaseMemory() override;
};

} // end namespace llvm

#endif // LLVM_CODEGEN_LIBCALLLOWERINGINFO_H
1 change: 1 addition & 0 deletions llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ LLVM_ABI void initializeGlobalMergeFuncPassWrapperPass(PassRegistry &);
LLVM_ABI void initializeGlobalMergePass(PassRegistry &);
LLVM_ABI void initializeGlobalsAAWrapperPassPass(PassRegistry &);
LLVM_ABI void initializeHardwareLoopsLegacyPass(PassRegistry &);
LLVM_ABI void initializeLibcallLoweringInfoWrapperPass(PassRegistry &);
LLVM_ABI void initializeMIRProfileLoaderPassPass(PassRegistry &);
LLVM_ABI void initializeIRSimilarityIdentifierWrapperPassPass(PassRegistry &);
LLVM_ABI void initializeIRTranslatorPass(PassRegistry &);
Expand Down
16 changes: 16 additions & 0 deletions llvm/lib/Analysis/RuntimeLibcallInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ using namespace llvm;

AnalysisKey RuntimeLibraryAnalysis::Key;

RuntimeLibraryAnalysis::RuntimeLibraryAnalysis(const Triple &TT,
ExceptionHandling ExceptionModel,
FloatABI::ABIType FloatABI,
EABI EABIVersion,
StringRef ABIName,
VectorLibrary VecLib)
: LibcallsInfo(std::in_place, TT, ExceptionModel, FloatABI, EABIVersion,
ABIName, VecLib) {}

RTLIB::RuntimeLibcallsInfo
RuntimeLibraryAnalysis::run(const Module &M, ModuleAnalysisManager &) {
if (!LibcallsInfo)
Expand All @@ -26,6 +35,13 @@ INITIALIZE_PASS(RuntimeLibraryInfoWrapper, "runtime-library-info",
RuntimeLibraryInfoWrapper::RuntimeLibraryInfoWrapper()
: ImmutablePass(ID), RTLA(RTLIB::RuntimeLibcallsInfo(Triple())) {}

RuntimeLibraryInfoWrapper::RuntimeLibraryInfoWrapper(
const Triple &TT, ExceptionHandling ExceptionModel,
FloatABI::ABIType FloatABI, EABI EABIVersion, StringRef ABIName,
VectorLibrary VecLib)
: ImmutablePass(ID), RTLCI(std::in_place, TT, ExceptionModel, FloatABI,
EABIVersion, ABIName, VecLib) {}

char RuntimeLibraryInfoWrapper::ID = 0;

ModulePass *llvm::createRuntimeLibraryInfoWrapperPass() {
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void llvm::initializeCodeGen(PassRegistry &Registry) {
initializeInterleavedLoadCombinePass(Registry);
initializeInterleavedAccessPass(Registry);
initializeJMCInstrumenterPass(Registry);
initializeLibcallLoweringInfoWrapperPass(Registry);
initializeLiveDebugValuesLegacyPass(Registry);
initializeLiveDebugVariablesWrapperLegacyPass(Registry);
initializeLiveIntervalsWrapperPassPass(Registry);
Expand Down
31 changes: 26 additions & 5 deletions llvm/lib/CodeGen/ExpandFp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ static void addToWorklist(Instruction &I,
}

static bool runImpl(Function &F, const TargetLowering &TLI,
AssumptionCache *AC) {
const LibcallLoweringInfo &Libcalls, AssumptionCache *AC) {
SmallVector<Instruction *, 4> Worklist;

unsigned MaxLegalFpConvertBitWidth =
Expand Down Expand Up @@ -1090,12 +1090,17 @@ class ExpandFpLegacyPass : public FunctionPass {

bool runOnFunction(Function &F) override {
auto *TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
auto *TLI = TM->getSubtargetImpl(F)->getTargetLowering();
const TargetSubtargetInfo *Subtarget = TM->getSubtargetImpl(F);
auto *TLI = Subtarget->getTargetLowering();
AssumptionCache *AC = nullptr;

const LibcallLoweringInfo &Libcalls =
getAnalysis<LibcallLoweringInfoWrapper>().getLibcallLowering(
*Subtarget);

if (OptLevel != CodeGenOptLevel::None && !F.hasOptNone())
AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
return runImpl(F, *TLI, AC);
return runImpl(F, *TLI, Libcalls, AC);
}

void getAnalysisUsage(AnalysisUsage &AU) const override {
Expand All @@ -1104,6 +1109,7 @@ class ExpandFpLegacyPass : public FunctionPass {
AU.addRequired<AssumptionCacheTracker>();
AU.addPreserved<AAResultsWrapperPass>();
AU.addPreserved<GlobalsAAWrapperPass>();
AU.addRequired<LibcallLoweringInfoWrapper>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Should it be added as INITIALIZE_PASS_DEPENDENCY as well? I still haven't figured out what's the difference between the macro and addRequired() call.

}
};
} // namespace
Expand All @@ -1126,8 +1132,23 @@ PreservedAnalyses ExpandFpPass::run(Function &F, FunctionAnalysisManager &FAM) {
AssumptionCache *AC = nullptr;
if (OptLevel != CodeGenOptLevel::None)
AC = &FAM.getResult<AssumptionAnalysis>(F);
return runImpl(F, TLI, AC) ? PreservedAnalyses::none()
: PreservedAnalyses::all();

auto &MAMProxy = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F);

const LibcallLoweringModuleAnalysisResult *LibcallLowering =
MAMProxy.getCachedResult<LibcallLoweringModuleAnalysis>(*F.getParent());

if (!LibcallLowering) {
F.getContext().emitError("'" + LibcallLoweringModuleAnalysis::name() +
"' analysis required");
return PreservedAnalyses::all();
}

const LibcallLoweringInfo &Libcalls =
LibcallLowering->getLibcallLowering(*STI);

return runImpl(F, TLI, Libcalls, AC) ? PreservedAnalyses::none()
: PreservedAnalyses::all();
}

char ExpandFpLegacyPass::ID = 0;
Expand Down
42 changes: 42 additions & 0 deletions llvm/lib/CodeGen/LibcallLoweringInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
//===----------------------------------------------------------------------===//

#include "llvm/CodeGen/LibcallLoweringInfo.h"
#include "llvm/Analysis/RuntimeLibcallInfo.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/InitializePasses.h"
#include "llvm/Target/TargetMachine.h"

using namespace llvm;

Expand All @@ -28,3 +31,42 @@ LibcallLoweringInfo::LibcallLoweringInfo(

Subtarget.initLibcallLoweringInfo(*this);
}

AnalysisKey LibcallLoweringModuleAnalysis::Key;

bool LibcallLoweringModuleAnalysisResult::invalidate(
Module &, const PreservedAnalyses &PA,
ModuleAnalysisManager::Invalidator &) {
// Passes that change the runtime libcall set must explicitly invalidate this
// pass.
auto PAC = PA.getChecker<LibcallLoweringModuleAnalysis>();
return !PAC.preservedWhenStateless();
}

LibcallLoweringModuleAnalysisResult
LibcallLoweringModuleAnalysis::run(Module &M, ModuleAnalysisManager &MAM) {
LibcallLoweringMap.init(&MAM.getResult<RuntimeLibraryAnalysis>(M));
return LibcallLoweringMap;
}

INITIALIZE_PASS_BEGIN(LibcallLoweringInfoWrapper, "libcall-lowering-info",
"Library Function Lowering Analysis", false, true)
INITIALIZE_PASS_DEPENDENCY(RuntimeLibraryInfoWrapper)
INITIALIZE_PASS_END(LibcallLoweringInfoWrapper, "libcall-lowering-info",
"Library Function Lowering Analysis", false, true)

char LibcallLoweringInfoWrapper::ID = 0;

LibcallLoweringInfoWrapper::LibcallLoweringInfoWrapper() : ImmutablePass(ID) {}

bool LibcallLoweringInfoWrapper::doInitialization(Module &M) {
Result.init(&getAnalysis<RuntimeLibraryInfoWrapper>().getRTLCI(M));
return false;
}

void LibcallLoweringInfoWrapper::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<RuntimeLibraryInfoWrapper>();
AU.setPreservesAll();
}

void LibcallLoweringInfoWrapper::releaseMemory() { Result.clear(); }
Loading
Loading