Skip to content

Commit 7762503

Browse files
Add Polly support. It is always compiled and linked, but not used by default.
Use can be triggered via `-C polly=true` or at compiler build-time (see `config.toml.example`). Force LLVM rebuild on buildbots.
1 parent 65d201f commit 7762503

File tree

11 files changed

+58
-11
lines changed

11 files changed

+58
-11
lines changed

config.toml.example

+3
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@
346346
# Whether to deny warnings in crates
347347
#deny-warnings = true
348348

349+
# Use polyhedral optimizations by default.
350+
#use-polly-by-default = false
351+
349352
# =============================================================================
350353
# Options for specific targets
351354
#

src/bootstrap/bootstrap.py

+4
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,10 @@ def update_submodules(self):
690690
recorded_submodules[data[3]] = data[2]
691691
for module in filtered_submodules:
692692
self.update_submodule(module[0], module[1], recorded_submodules)
693+
polly_path = "src/llvm/tools/polly"
694+
if not os.path.exists(os.path.join(self.rust_root, polly_path)):
695+
run(["git", "clone", "https://github.com./llvm-mirror/polly.git",
696+
"src/llvm/tools/polly", "-b", "release_60"])
693697
print("Submodules updated in %.2f seconds" % (time() - start_time))
694698

695699
def set_dev_environment(self):

src/bootstrap/compile.rs

+3
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,9 @@ fn rustc_cargo_env(builder: &Builder, cargo: &mut Command) {
553553
if builder.config.rustc_parallel_queries {
554554
cargo.env("RUSTC_PARALLEL_QUERIES", "1");
555555
}
556+
if builder.config.rust_codegen_polly_by_default {
557+
cargo.env("RUSTC_FORCE_USE_POLLY", "1");
558+
}
556559
}
557560

558561
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]

src/bootstrap/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub struct Config {
103103
pub rust_dist_src: bool,
104104
pub rust_codegen_backends: Vec<Interned<String>>,
105105
pub rust_codegen_backends_dir: String,
106+
pub rust_codegen_polly_by_default: bool,
106107

107108
pub build: Interned<String>,
108109
pub hosts: Vec<Interned<String>>,
@@ -306,6 +307,7 @@ struct Rust {
306307
wasm_syscall: Option<bool>,
307308
lld: Option<bool>,
308309
deny_warnings: Option<bool>,
310+
use_polly_by_default: Option<bool>,
309311
}
310312

311313
/// TOML representation of how each build target is configured.
@@ -537,6 +539,10 @@ impl Config {
537539
Some(n) => config.rust_codegen_units = Some(n),
538540
None => {}
539541
}
542+
543+
config.rust_codegen_polly_by_default = rust
544+
.use_polly_by_default
545+
.unwrap_or(false);
540546
}
541547

542548
if let Some(ref t) = toml.target {

src/librustc/session/config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,9 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
10741074
[TRACKED], "panic strategy to compile crate with"),
10751075
incremental: Option<String> = (None, parse_opt_string, [UNTRACKED],
10761076
"enable incremental compilation"),
1077+
polly: bool = (option_env!("RUSTC_FORCE_USE_POLLY").map(|_| true ).unwrap_or(false),
1078+
parse_bool, [UNTRACKED], "Run the Polly polyhedral \
1079+
model optimization passes."),
10771080
}
10781081

10791082
options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,

src/librustc_llvm/build.rs

+3
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ fn main() {
208208
println!("cargo:rustc-link-lib={}={}", kind, name);
209209
}
210210

211+
println!("cargo:rustc-link-lib=static=Polly");
212+
println!("cargo:rustc-link-lib=static=PollyISL");
213+
211214
// LLVM ldflags
212215
//
213216
// If we're a cross-compile of LLVM then unfortunately we can't trust these

src/librustc_llvm/ffi.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,8 @@ extern "C" {
16421642
Singlethread: bool)
16431643
-> TargetMachineRef;
16441644
pub fn LLVMRustDisposeTargetMachine(T: TargetMachineRef);
1645-
pub fn LLVMRustAddAnalysisPasses(T: TargetMachineRef, PM: PassManagerRef, M: ModuleRef);
1645+
pub fn LLVMRustAddAnalysisPasses(T: TargetMachineRef, PM: PassManagerRef, M: ModuleRef,
1646+
Polly: bool);
16461647
pub fn LLVMRustAddBuilderLibraryInfo(PMB: PassManagerBuilderRef,
16471648
M: ModuleRef,
16481649
DisableSimplifyLibCalls: bool);

src/librustc_trans/back/lto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ fn run_pass_manager(cgcx: &CodegenContext,
460460
debug!("running the pass manager");
461461
unsafe {
462462
let pm = llvm::LLVMCreatePassManager();
463-
llvm::LLVMRustAddAnalysisPasses(tm, pm, llmod);
463+
llvm::LLVMRustAddAnalysisPasses(tm, pm, llmod, config.polly);
464464
let pass = llvm::LLVMRustFindAndCreatePass("verify\0".as_ptr() as *const _);
465465
assert!(!pass.is_null());
466466
llvm::LLVMRustAddPass(pm, pass);

src/librustc_trans/back/write.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ pub struct ModuleConfig {
271271
no_integrated_as: bool,
272272
embed_bitcode: bool,
273273
embed_bitcode_marker: bool,
274+
pub polly: bool,
274275
}
275276

276277
impl ModuleConfig {
@@ -302,7 +303,8 @@ impl ModuleConfig {
302303
vectorize_loop: false,
303304
vectorize_slp: false,
304305
merge_functions: false,
305-
inline_threshold: None
306+
inline_threshold: None,
307+
polly: false,
306308
}
307309
}
308310

@@ -340,6 +342,8 @@ impl ModuleConfig {
340342

341343
self.merge_functions = sess.opts.optimize == config::OptLevel::Default ||
342344
sess.opts.optimize == config::OptLevel::Aggressive;
345+
self.polly = sess.opts.cg.polly && !self.no_prepopulate_passes &&
346+
!sess.target.target.options.is_like_emscripten;
343347
}
344348
}
345349

@@ -567,8 +571,8 @@ unsafe fn optimize(cgcx: &CodegenContext,
567571

568572
if !config.no_verify { assert!(addpass("verify")); }
569573
if !config.no_prepopulate_passes {
570-
llvm::LLVMRustAddAnalysisPasses(tm, fpm, llmod);
571-
llvm::LLVMRustAddAnalysisPasses(tm, mpm, llmod);
574+
llvm::LLVMRustAddAnalysisPasses(tm, fpm, llmod, config.polly);
575+
llvm::LLVMRustAddAnalysisPasses(tm, mpm, llmod, config.polly);
572576
let opt_level = config.opt_level.unwrap_or(llvm::CodeGenOptLevel::None);
573577
with_llvm_pmb(llmod, &config, opt_level, &mut |b| {
574578
llvm::LLVMPassManagerBuilderPopulateFunctionPassManager(b, fpm);
@@ -666,11 +670,12 @@ unsafe fn codegen(cgcx: &CodegenContext,
666670
unsafe fn with_codegen<F, R>(tm: TargetMachineRef,
667671
llmod: ModuleRef,
668672
no_builtins: bool,
673+
polly: bool,
669674
f: F) -> R
670675
where F: FnOnce(PassManagerRef) -> R,
671676
{
672677
let cpm = llvm::LLVMCreatePassManager();
673-
llvm::LLVMRustAddAnalysisPasses(tm, cpm, llmod);
678+
llvm::LLVMRustAddAnalysisPasses(tm, cpm, llmod, polly);
674679
llvm::LLVMRustAddLibraryInfo(cpm, llmod, no_builtins);
675680
f(cpm)
676681
}
@@ -765,7 +770,8 @@ unsafe fn codegen(cgcx: &CodegenContext,
765770
cursor.position() as size_t
766771
}
767772

768-
with_codegen(tm, llmod, config.no_builtins, |cpm| {
773+
with_codegen(tm, llmod, config.no_builtins, config.polly,
774+
|cpm| {
769775
llvm::LLVMRustPrintModule(cpm, llmod, out.as_ptr(), demangle_callback);
770776
llvm::LLVMDisposePassManager(cpm);
771777
});
@@ -783,7 +789,8 @@ unsafe fn codegen(cgcx: &CodegenContext,
783789
} else {
784790
llmod
785791
};
786-
with_codegen(tm, llmod, config.no_builtins, |cpm| {
792+
with_codegen(tm, llmod, config.no_builtins, config.polly,
793+
|cpm| {
787794
write_output_file(diag_handler, tm, cpm, llmod, &path,
788795
llvm::FileType::AssemblyFile)
789796
})?;
@@ -794,7 +801,8 @@ unsafe fn codegen(cgcx: &CodegenContext,
794801
}
795802

796803
if write_obj {
797-
with_codegen(tm, llmod, config.no_builtins, |cpm| {
804+
with_codegen(tm, llmod, config.no_builtins, config.polly,
805+
|cpm| {
798806
write_output_file(diag_handler, tm, cpm, llmod, &obj_out,
799807
llvm::FileType::ObjectFile)
800808
})?;

src/rustllvm/PassWrapper.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ DEFINE_STDCXX_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
6161
DEFINE_STDCXX_CONVERSION_FUNCTIONS(PassManagerBuilder,
6262
LLVMPassManagerBuilderRef)
6363

64+
65+
namespace polly {
66+
void initializePollyPasses(llvm::PassRegistry &Registry);
67+
}
68+
6469
extern "C" void LLVMInitializePasses() {
6570
PassRegistry &Registry = *PassRegistry::getPassRegistry();
6671
initializeCore(Registry);
@@ -73,6 +78,8 @@ extern "C" void LLVMInitializePasses() {
7378
initializeInstCombine(Registry);
7479
initializeInstrumentation(Registry);
7580
initializeTarget(Registry);
81+
82+
polly::initializePollyPasses(Registry);
7683
}
7784

7885
enum class LLVMRustPassKind {
@@ -415,15 +422,24 @@ extern "C" void LLVMRustDisposeTargetMachine(LLVMTargetMachineRef TM) {
415422
delete unwrap(TM);
416423
}
417424

425+
namespace polly {
426+
void registerPollyPasses(llvm::legacy::PassManagerBase &PM);
427+
}
428+
418429
// Unfortunately, LLVM doesn't expose a C API to add the corresponding analysis
419430
// passes for a target to a pass manager. We export that functionality through
420431
// this function.
421432
extern "C" void LLVMRustAddAnalysisPasses(LLVMTargetMachineRef TM,
422433
LLVMPassManagerRef PMR,
423-
LLVMModuleRef M) {
434+
LLVMModuleRef M,
435+
bool Polly) {
424436
PassManagerBase *PM = unwrap(PMR);
425437
PM->add(
426438
createTargetTransformInfoWrapperPass(unwrap(TM)->getTargetIRAnalysis()));
439+
440+
if(Polly) {
441+
polly::registerPollyPasses(*PM);
442+
}
427443
}
428444

429445
extern "C" void LLVMRustConfigurePassManagerBuilder(

src/rustllvm/llvm-rebuild-trigger

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# If this file is modified, then llvm will be (optionally) cleaned and then rebuilt.
22
# The actual contents of this file do not matter, but to trigger a change on the
33
# build bots then the contents should be changed so git updates the mtime.
4-
2018-04-05
4+
2018-04-18

0 commit comments

Comments
 (0)