Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 55f345f

Browse files
committed
Support LLVM 12 in rustc
1 parent 7c3a914 commit 55f345f

File tree

18 files changed

+226
-77
lines changed

18 files changed

+226
-77
lines changed

compiler/rustc_codegen_llvm/src/abi.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,13 @@ impl<'tcx> FnAbiLlvmExt<'tcx> for FnAbi<'tcx, Ty<'tcx>> {
430430
PassMode::Indirect { ref attrs, extra_attrs: _, on_stack } => {
431431
assert!(!on_stack);
432432
let i = apply(attrs);
433-
llvm::Attribute::StructRet.apply_llfn(llvm::AttributePlace::Argument(i), llfn);
433+
unsafe {
434+
llvm::LLVMRustAddStructRetAttr(
435+
llfn,
436+
llvm::AttributePlace::Argument(i).as_uint(),
437+
self.ret.layout.llvm_type(cx),
438+
);
439+
}
434440
}
435441
_ => {}
436442
}
@@ -486,8 +492,13 @@ impl<'tcx> FnAbiLlvmExt<'tcx> for FnAbi<'tcx, Ty<'tcx>> {
486492
PassMode::Indirect { ref attrs, extra_attrs: _, on_stack } => {
487493
assert!(!on_stack);
488494
let i = apply(attrs);
489-
llvm::Attribute::StructRet
490-
.apply_callsite(llvm::AttributePlace::Argument(i), callsite);
495+
unsafe {
496+
llvm::LLVMRustAddStructRetCallSiteAttr(
497+
callsite,
498+
llvm::AttributePlace::Argument(i).as_uint(),
499+
self.ret.layout.llvm_type(bx),
500+
);
501+
}
491502
}
492503
_ => {}
493504
}

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ fn strip_x86_address_spaces(data_layout: String) -> String {
104104
data_layout.replace("-p270:32:32-p271:32:32-p272:64:64-", "-")
105105
}
106106

107+
fn strip_powerpc64_vectors(data_layout: String) -> String {
108+
data_layout.replace("-v256:256:256-v512:512:512", "")
109+
}
110+
107111
pub unsafe fn create_module(
108112
tcx: TyCtxt<'_>,
109113
llcx: &'ll llvm::Context,
@@ -119,6 +123,9 @@ pub unsafe fn create_module(
119123
{
120124
target_data_layout = strip_x86_address_spaces(target_data_layout);
121125
}
126+
if llvm_util::get_version() < (12, 0, 0) && sess.target.arch == "powerpc64" {
127+
target_data_layout = strip_powerpc64_vectors(target_data_layout);
128+
}
122129

123130
// Ensure the data-layout values hardcoded remain the defaults.
124131
if sess.target.is_builtin {

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ pub enum TypeKind {
239239
Token = 16,
240240
ScalableVector = 17,
241241
BFloat = 18,
242+
X86_AMX = 19,
242243
}
243244

244245
impl TypeKind {
@@ -263,6 +264,7 @@ impl TypeKind {
263264
TypeKind::Token => rustc_codegen_ssa::common::TypeKind::Token,
264265
TypeKind::ScalableVector => rustc_codegen_ssa::common::TypeKind::ScalableVector,
265266
TypeKind::BFloat => rustc_codegen_ssa::common::TypeKind::BFloat,
267+
TypeKind::X86_AMX => rustc_codegen_ssa::common::TypeKind::X86_AMX,
266268
}
267269
}
268270
}
@@ -1073,6 +1075,7 @@ extern "C" {
10731075
pub fn LLVMRustAddDereferenceableAttr(Fn: &Value, index: c_uint, bytes: u64);
10741076
pub fn LLVMRustAddDereferenceableOrNullAttr(Fn: &Value, index: c_uint, bytes: u64);
10751077
pub fn LLVMRustAddByValAttr(Fn: &Value, index: c_uint, ty: &Type);
1078+
pub fn LLVMRustAddStructRetAttr(Fn: &Value, index: c_uint, ty: &Type);
10761079
pub fn LLVMRustAddFunctionAttribute(Fn: &Value, index: c_uint, attr: Attribute);
10771080
pub fn LLVMRustAddFunctionAttrStringValue(
10781081
Fn: &Value,
@@ -1108,6 +1111,7 @@ extern "C" {
11081111
pub fn LLVMRustAddDereferenceableCallSiteAttr(Instr: &Value, index: c_uint, bytes: u64);
11091112
pub fn LLVMRustAddDereferenceableOrNullCallSiteAttr(Instr: &Value, index: c_uint, bytes: u64);
11101113
pub fn LLVMRustAddByValCallSiteAttr(Instr: &Value, index: c_uint, ty: &Type);
1114+
pub fn LLVMRustAddStructRetCallSiteAttr(Instr: &Value, index: c_uint, ty: &Type);
11111115

11121116
// Operations on load/store instructions (only)
11131117
pub fn LLVMSetVolatile(MemoryAccessInst: &Value, volatile: Bool);

compiler/rustc_codegen_ssa/src/common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pub enum TypeKind {
9595
Token,
9696
ScalableVector,
9797
BFloat,
98+
X86_AMX,
9899
}
99100

100101
// FIXME(mw): Anything that is produced via DepGraph::with_task() must implement

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

Lines changed: 97 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "LLVMWrapper.h"
77

8+
#include "llvm/Analysis/AliasAnalysis.h"
89
#include "llvm/Analysis/TargetLibraryInfo.h"
910
#include "llvm/Analysis/TargetTransformInfo.h"
1011
#include "llvm/CodeGen/TargetSubtargetInfo.h"
@@ -683,6 +684,25 @@ void LLVMSelfProfileInitializeCallbacks(
683684
PassInstrumentationCallbacks& PIC, void* LlvmSelfProfiler,
684685
LLVMRustSelfProfileBeforePassCallback BeforePassCallback,
685686
LLVMRustSelfProfileAfterPassCallback AfterPassCallback) {
687+
#if LLVM_VERSION_GE(12, 0)
688+
PIC.registerBeforeNonSkippedPassCallback([LlvmSelfProfiler, BeforePassCallback](
689+
StringRef Pass, llvm::Any Ir) {
690+
std::string PassName = Pass.str();
691+
std::string IrName = LLVMRustwrappedIrGetName(Ir);
692+
BeforePassCallback(LlvmSelfProfiler, PassName.c_str(), IrName.c_str());
693+
});
694+
695+
PIC.registerAfterPassCallback(
696+
[LlvmSelfProfiler, AfterPassCallback](StringRef Pass, llvm::Any IR,
697+
const PreservedAnalyses &Preserved) {
698+
AfterPassCallback(LlvmSelfProfiler);
699+
});
700+
701+
PIC.registerAfterPassInvalidatedCallback(
702+
[LlvmSelfProfiler, AfterPassCallback](StringRef Pass, const PreservedAnalyses &Preserved) {
703+
AfterPassCallback(LlvmSelfProfiler);
704+
});
705+
#else
686706
PIC.registerBeforePassCallback([LlvmSelfProfiler, BeforePassCallback](
687707
StringRef Pass, llvm::Any Ir) {
688708
std::string PassName = Pass.str();
@@ -700,6 +720,7 @@ void LLVMSelfProfileInitializeCallbacks(
700720
[LlvmSelfProfiler, AfterPassCallback](StringRef Pass) {
701721
AfterPassCallback(LlvmSelfProfiler);
702722
});
723+
#endif
703724

704725
PIC.registerBeforeAnalysisCallback([LlvmSelfProfiler, BeforePassCallback](
705726
StringRef Pass, llvm::Any Ir) {
@@ -760,8 +781,15 @@ LLVMRustOptimizeWithNewPassManager(
760781
PTO.LoopVectorization = LoopVectorize;
761782
PTO.SLPVectorization = SLPVectorize;
762783

784+
// FIXME: We may want to expose this as an option.
785+
bool DebugPassManager = false;
786+
763787
PassInstrumentationCallbacks PIC;
788+
#if LLVM_VERSION_GE(12, 0)
789+
StandardInstrumentations SI(DebugPassManager);
790+
#else
764791
StandardInstrumentations SI;
792+
#endif
765793
SI.registerCallbacks(PIC);
766794

767795
if (LlvmSelfProfiler){
@@ -777,10 +805,12 @@ LLVMRustOptimizeWithNewPassManager(
777805
PGOOpt = PGOOptions(PGOUsePath, "", "", PGOOptions::IRUse);
778806
}
779807

808+
#if LLVM_VERSION_GE(12, 0)
809+
PassBuilder PB(DebugPassManager, TM, PTO, PGOOpt, &PIC);
810+
#else
780811
PassBuilder PB(TM, PTO, PGOOpt, &PIC);
812+
#endif
781813

782-
// FIXME: We may want to expose this as an option.
783-
bool DebugPassManager = false;
784814
LoopAnalysisManager LAM(DebugPassManager);
785815
FunctionAnalysisManager FAM(DebugPassManager);
786816
CGSCCAnalysisManager CGAM(DebugPassManager);
@@ -802,7 +832,8 @@ LLVMRustOptimizeWithNewPassManager(
802832

803833
// We manually collect pipeline callbacks so we can apply them at O0, where the
804834
// PassBuilder does not create a pipeline.
805-
std::vector<std::function<void(ModulePassManager &)>> PipelineStartEPCallbacks;
835+
std::vector<std::function<void(ModulePassManager &, PassBuilder::OptimizationLevel)>>
836+
PipelineStartEPCallbacks;
806837
#if LLVM_VERSION_GE(11, 0)
807838
std::vector<std::function<void(ModulePassManager &, PassBuilder::OptimizationLevel)>>
808839
OptimizerLastEPCallbacks;
@@ -812,9 +843,11 @@ LLVMRustOptimizeWithNewPassManager(
812843
#endif
813844

814845
if (VerifyIR) {
815-
PipelineStartEPCallbacks.push_back([VerifyIR](ModulePassManager &MPM) {
846+
PipelineStartEPCallbacks.push_back(
847+
[VerifyIR](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
816848
MPM.addPass(VerifierPass());
817-
});
849+
}
850+
);
818851
}
819852

820853
if (SanitizerOptions) {
@@ -832,9 +865,11 @@ LLVMRustOptimizeWithNewPassManager(
832865
);
833866
#else
834867
#if LLVM_VERSION_GE(10, 0)
835-
PipelineStartEPCallbacks.push_back([Options](ModulePassManager &MPM) {
836-
MPM.addPass(MemorySanitizerPass(Options));
837-
});
868+
PipelineStartEPCallbacks.push_back(
869+
[Options](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
870+
MPM.addPass(MemorySanitizerPass(Options));
871+
}
872+
);
838873
#endif
839874
OptimizerLastEPCallbacks.push_back(
840875
[Options](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
@@ -854,9 +889,11 @@ LLVMRustOptimizeWithNewPassManager(
854889
);
855890
#else
856891
#if LLVM_VERSION_GE(10, 0)
857-
PipelineStartEPCallbacks.push_back([](ModulePassManager &MPM) {
858-
MPM.addPass(ThreadSanitizerPass());
859-
});
892+
PipelineStartEPCallbacks.push_back(
893+
[](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
894+
MPM.addPass(ThreadSanitizerPass());
895+
}
896+
);
860897
#endif
861898
OptimizerLastEPCallbacks.push_back(
862899
[](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
@@ -879,9 +916,11 @@ LLVMRustOptimizeWithNewPassManager(
879916
}
880917
);
881918
#else
882-
PipelineStartEPCallbacks.push_back([&](ModulePassManager &MPM) {
883-
MPM.addPass(RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
884-
});
919+
PipelineStartEPCallbacks.push_back(
920+
[&](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
921+
MPM.addPass(RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
922+
}
923+
);
885924
OptimizerLastEPCallbacks.push_back(
886925
[SanitizerOptions](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
887926
FPM.addPass(AddressSanitizerPass(
@@ -890,7 +929,7 @@ LLVMRustOptimizeWithNewPassManager(
890929
}
891930
);
892931
PipelineStartEPCallbacks.push_back(
893-
[SanitizerOptions](ModulePassManager &MPM) {
932+
[SanitizerOptions](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
894933
MPM.addPass(ModuleAddressSanitizerPass(
895934
/*CompileKernel=*/false, SanitizerOptions->SanitizeAddressRecover));
896935
}
@@ -907,7 +946,7 @@ LLVMRustOptimizeWithNewPassManager(
907946
);
908947
#else
909948
PipelineStartEPCallbacks.push_back(
910-
[SanitizerOptions](ModulePassManager &MPM) {
949+
[SanitizerOptions](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
911950
MPM.addPass(HWAddressSanitizerPass(
912951
/*CompileKernel=*/false, SanitizerOptions->SanitizeHWAddressRecover));
913952
}
@@ -917,35 +956,53 @@ LLVMRustOptimizeWithNewPassManager(
917956
}
918957

919958
ModulePassManager MPM(DebugPassManager);
959+
bool NeedThinLTOBufferPasses = UseThinLTOBuffers;
920960
if (!NoPrepopulatePasses) {
921961
if (OptLevel == PassBuilder::OptimizationLevel::O0) {
962+
#if LLVM_VERSION_GE(12, 0)
922963
for (const auto &C : PipelineStartEPCallbacks)
923-
C(MPM);
964+
PB.registerPipelineStartEPCallback(C);
965+
for (const auto &C : OptimizerLastEPCallbacks)
966+
PB.registerOptimizerLastEPCallback(C);
924967

925-
#if LLVM_VERSION_GE(11, 0)
968+
// Pass false as we manually schedule ThinLTOBufferPasses below.
969+
MPM = PB.buildO0DefaultPipeline(OptLevel, /* PreLinkLTO */ false);
970+
#else
971+
for (const auto &C : PipelineStartEPCallbacks)
972+
C(MPM, OptLevel);
973+
974+
# if LLVM_VERSION_GE(11, 0)
926975
for (const auto &C : OptimizerLastEPCallbacks)
927976
C(MPM, OptLevel);
928-
#else
977+
# else
929978
if (!OptimizerLastEPCallbacks.empty()) {
930979
FunctionPassManager FPM(DebugPassManager);
931980
for (const auto &C : OptimizerLastEPCallbacks)
932981
C(FPM, OptLevel);
933982
MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
934983
}
935-
#endif
984+
# endif
936985

937986
MPM.addPass(AlwaysInlinerPass(EmitLifetimeMarkers));
938987

939-
#if LLVM_VERSION_GE(10, 0)
988+
# if LLVM_VERSION_GE(10, 0)
940989
if (PGOOpt) {
941990
PB.addPGOInstrPassesForO0(
942991
MPM, DebugPassManager, PGOOpt->Action == PGOOptions::IRInstr,
943992
/*IsCS=*/false, PGOOpt->ProfileFile, PGOOpt->ProfileRemappingFile);
944993
}
994+
# endif
945995
#endif
946996
} else {
997+
#if LLVM_VERSION_GE(12, 0)
947998
for (const auto &C : PipelineStartEPCallbacks)
948999
PB.registerPipelineStartEPCallback(C);
1000+
#else
1001+
for (const auto &C : PipelineStartEPCallbacks)
1002+
PB.registerPipelineStartEPCallback([C, OptLevel](ModulePassManager &MPM) {
1003+
C(MPM, OptLevel);
1004+
});
1005+
#endif
9491006
if (OptStage != LLVMRustOptStage::PreLinkThinLTO) {
9501007
for (const auto &C : OptimizerLastEPCallbacks)
9511008
PB.registerOptimizerLastEPCallback(C);
@@ -956,7 +1013,12 @@ LLVMRustOptimizeWithNewPassManager(
9561013
MPM = PB.buildPerModuleDefaultPipeline(OptLevel, DebugPassManager);
9571014
break;
9581015
case LLVMRustOptStage::PreLinkThinLTO:
1016+
#if LLVM_VERSION_GE(12, 0)
1017+
MPM = PB.buildThinLTOPreLinkDefaultPipeline(OptLevel);
1018+
NeedThinLTOBufferPasses = false;
1019+
#else
9591020
MPM = PB.buildThinLTOPreLinkDefaultPipeline(OptLevel, DebugPassManager);
1021+
#endif
9601022
#if LLVM_VERSION_GE(11, 0)
9611023
for (const auto &C : OptimizerLastEPCallbacks)
9621024
C(MPM, OptLevel);
@@ -970,21 +1032,34 @@ LLVMRustOptimizeWithNewPassManager(
9701032
#endif
9711033
break;
9721034
case LLVMRustOptStage::PreLinkFatLTO:
1035+
#if LLVM_VERSION_GE(12, 0)
1036+
MPM = PB.buildLTOPreLinkDefaultPipeline(OptLevel);
1037+
NeedThinLTOBufferPasses = false;
1038+
#else
9731039
MPM = PB.buildLTOPreLinkDefaultPipeline(OptLevel, DebugPassManager);
1040+
#endif
9741041
break;
9751042
case LLVMRustOptStage::ThinLTO:
9761043
// FIXME: Does it make sense to pass the ModuleSummaryIndex?
9771044
// It only seems to be needed for C++ specific optimizations.
1045+
#if LLVM_VERSION_GE(12, 0)
1046+
MPM = PB.buildThinLTODefaultPipeline(OptLevel, nullptr);
1047+
#else
9781048
MPM = PB.buildThinLTODefaultPipeline(OptLevel, DebugPassManager, nullptr);
1049+
#endif
9791050
break;
9801051
case LLVMRustOptStage::FatLTO:
1052+
#if LLVM_VERSION_GE(12, 0)
1053+
MPM = PB.buildLTODefaultPipeline(OptLevel, nullptr);
1054+
#else
9811055
MPM = PB.buildLTODefaultPipeline(OptLevel, DebugPassManager, nullptr);
1056+
#endif
9821057
break;
9831058
}
9841059
}
9851060
}
9861061

987-
if (UseThinLTOBuffers) {
1062+
if (NeedThinLTOBufferPasses) {
9881063
MPM.addPass(CanonicalizeAliasesPass());
9891064
MPM.addPass(NameAnonGlobalPass());
9901065
}

0 commit comments

Comments
 (0)