summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp')
-rw-r--r--compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp85
1 files changed, 21 insertions, 64 deletions
diff --git a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
index 31565db1b..6fd0c9014 100644
--- a/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
+++ b/compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
@@ -37,7 +37,9 @@
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
#include "llvm/LTO/LTO.h"
#include "llvm/Bitcode/BitcodeWriter.h"
-
+#if LLVM_VERSION_GE(18, 0)
+#include "llvm/TargetParser/Host.h"
+#endif
#include "llvm/Transforms/Instrumentation.h"
#include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
#include "llvm/Support/TimeProfiler.h"
@@ -60,17 +62,17 @@ typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
DEFINE_STDCXX_CONVERSION_FUNCTIONS(Pass, LLVMPassRef)
DEFINE_STDCXX_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
-extern "C" void LLVMTimeTraceProfilerInitialize() {
+extern "C" void LLVMRustTimeTraceProfilerInitialize() {
timeTraceProfilerInitialize(
/* TimeTraceGranularity */ 0,
/* ProcName */ "rustc");
}
-extern "C" void LLVMTimeTraceProfilerFinishThread() {
+extern "C" void LLVMRustTimeTraceProfilerFinishThread() {
timeTraceProfilerFinishThread();
}
-extern "C" void LLVMTimeTraceProfilerFinish(const char* FileName) {
+extern "C" void LLVMRustTimeTraceProfilerFinish(const char* FileName) {
StringRef FN(FileName);
std::error_code EC;
raw_fd_ostream OS(FN, EC, sys::fs::CD_CreateAlways);
@@ -204,11 +206,7 @@ enum class LLVMRustCodeModel {
None,
};
-#if LLVM_VERSION_LT(16, 0)
-static Optional<CodeModel::Model>
-#else
static std::optional<CodeModel::Model>
-#endif
fromRust(LLVMRustCodeModel Model) {
switch (Model) {
case LLVMRustCodeModel::Tiny:
@@ -222,11 +220,7 @@ fromRust(LLVMRustCodeModel Model) {
case LLVMRustCodeModel::Large:
return CodeModel::Large;
case LLVMRustCodeModel::None:
-#if LLVM_VERSION_LT(16, 0)
- return None;
-#else
return std::nullopt;
-#endif
default:
report_fatal_error("Bad CodeModel.");
}
@@ -418,7 +412,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
const char *SplitDwarfFile,
const char *OutputObjFile,
const char *DebugInfoCompression,
- bool ForceEmulatedTls,
+ bool UseEmulatedTls,
const char *ArgsCstrBuff, size_t ArgsCstrBuffLen) {
auto OptLevel = fromRust(RustOptLevel);
@@ -452,7 +446,6 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
if (OutputObjFile) {
Options.ObjectFilenameForDebug = OutputObjFile;
}
-#if LLVM_VERSION_GE(16, 0)
if (!strcmp("zlib", DebugInfoCompression) && llvm::compression::zlib::isAvailable()) {
Options.CompressDebugSections = DebugCompressionType::Zlib;
} else if (!strcmp("zstd", DebugInfoCompression) && llvm::compression::zstd::isAvailable()) {
@@ -460,19 +453,14 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
} else if (!strcmp("none", DebugInfoCompression)) {
Options.CompressDebugSections = DebugCompressionType::None;
}
-#endif
Options.RelaxELFRelocations = RelaxELFRelocations;
Options.UseInitArray = UseInitArray;
#if LLVM_VERSION_LT(17, 0)
- if (ForceEmulatedTls) {
- Options.ExplicitEmulatedTLS = true;
- Options.EmulatedTLS = true;
- }
-#else
- Options.EmulatedTLS = ForceEmulatedTls || Trip.hasDefaultEmulatedTLS();
+ Options.ExplicitEmulatedTLS = true;
#endif
+ Options.EmulatedTLS = UseEmulatedTls;
if (TrapUnreachable) {
// Tell LLVM to codegen `unreachable` into an explicit trap instruction.
@@ -480,6 +468,14 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
// it prevents control flow from "falling through" into whatever code
// happens to be laid out next in memory.
Options.TrapUnreachable = true;
+ // But don't emit traps after other traps or no-returns unnecessarily.
+ // ...except for when targeting WebAssembly, because the NoTrapAfterNoreturn
+ // option causes bugs in the LLVM WebAssembly backend. You should be able to
+ // remove this check when Rust's minimum supported LLVM version is >= 18
+ // https://github.com/llvm/llvm-project/pull/65876
+ if (!Trip.isWasm()) {
+ Options.NoTrapAfterNoreturn = true;
+ }
}
if (Singlethread) {
@@ -734,22 +730,14 @@ LLVMRustOptimize(
bool DebugPassManager = false;
PassInstrumentationCallbacks PIC;
-#if LLVM_VERSION_LT(16, 0)
- StandardInstrumentations SI(DebugPassManager);
-#else
StandardInstrumentations SI(TheModule->getContext(), DebugPassManager);
-#endif
SI.registerCallbacks(PIC);
if (LlvmSelfProfiler){
LLVMSelfProfileInitializeCallbacks(PIC,LlvmSelfProfiler,BeforePassCallback,AfterPassCallback);
}
-#if LLVM_VERSION_LT(16, 0)
- Optional<PGOOptions> PGOOpt;
-#else
std::optional<PGOOptions> PGOOpt;
-#endif
#if LLVM_VERSION_GE(17, 0)
auto FS = vfs::getRealFileSystem();
#endif
@@ -868,7 +856,11 @@ LLVMRustOptimize(
// cargo run tests in multhreading mode by default
// so use atomics for coverage counters
Options.Atomic = true;
+#if LLVM_VERSION_GE(18, 0)
+ MPM.addPass(InstrProfilingLoweringPass(Options, false));
+#else
MPM.addPass(InstrProfiling(Options, false));
+#endif
}
);
}
@@ -882,12 +874,7 @@ LLVMRustOptimize(
/*EagerChecks=*/true);
OptimizerLastEPCallbacks.push_back(
[Options](ModulePassManager &MPM, OptimizationLevel Level) {
-#if LLVM_VERSION_LT(16, 0)
- MPM.addPass(ModuleMemorySanitizerPass(Options));
- MPM.addPass(createModuleToFunctionPassAdaptor(MemorySanitizerPass(Options)));
-#else
MPM.addPass(MemorySanitizerPass(Options));
-#endif
}
);
}
@@ -912,11 +899,7 @@ LLVMRustOptimize(
/*UseAfterScope=*/true,
AsanDetectStackUseAfterReturnMode::Runtime,
};
-#if LLVM_VERSION_LT(16, 0)
- MPM.addPass(ModuleAddressSanitizerPass(opts));
-#else
MPM.addPass(AddressSanitizerPass(opts));
-#endif
}
);
}
@@ -1562,32 +1545,6 @@ LLVMRustParseBitcodeForLTO(LLVMContextRef Context,
return wrap(std::move(*SrcOrError).release());
}
-// Find the bitcode section in the object file data and return it as a slice.
-// Fail if the bitcode section is present but empty.
-//
-// On success, the return value is the pointer to the start of the slice and
-// `out_len` is filled with the (non-zero) length. On failure, the return value
-// is `nullptr` and `out_len` is set to zero.
-extern "C" const char*
-LLVMRustGetBitcodeSliceFromObjectData(const char *data,
- size_t len,
- size_t *out_len) {
- *out_len = 0;
-
- StringRef Data(data, len);
- MemoryBufferRef Buffer(Data, ""); // The id is unused.
-
- Expected<MemoryBufferRef> BitcodeOrError =
- object::IRObjectFile::findBitcodeInMemBuffer(Buffer);
- if (!BitcodeOrError) {
- LLVMRustSetLastError(toString(BitcodeOrError.takeError()).c_str());
- return nullptr;
- }
-
- *out_len = BitcodeOrError->getBufferSize();
- return BitcodeOrError->getBufferStart();
-}
-
// Find a section of an object file by name. Fail if the section is missing or
// empty.
extern "C" const char *LLVMRustGetSliceFromObjectDataByName(const char *data,