-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod.rs
967 lines (841 loc) · 29 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
//! The main entry point to codegen. This worker thread is responsible for
//! initializing a Rust compiler driver and tricking it into just running
//! codegen on whatever kernel we need to compile.
//!
//! This thread also stores a cache of previously completed codegens. It
//! used to be stored in the Context, however now that `KernelDesc` is
//! parameterized by the PlatformCodegen trait, it makes more sense to
//! store it here.
//! TODO the cache is in memory only. It would be a good idea to be able
//! write it to disk to free some memory.
//!
use std::any::Any;
use std::collections::{BTreeMap, };
use std::io::{self, };
use std::mem::{self, drop, };
use std::sync::mpsc::{channel, Sender, Receiver, RecvTimeoutError, };
use std::sync::{Arc, Weak, Once, };
use std::time::Duration;
use rustc_ast::ast;
use rustc_middle;
use rustc_middle::arena::Arena;
use rustc_middle::middle::cstore::EncodedMetadata;
use rustc_middle::middle::exported_symbols::{SymbolExportLevel, };
use rustc_middle::mir::{CustomIntrinsicMirGen, };
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, TyCtxt, subst::SubstsRef, };
use rustc_session::Session;
use rustc_session::config::OutputFilenames;
use rustc_data_structures::fx::{FxHashMap};
use rustc_data_structures::sync::{Lrc, WorkerLocal, };
use rustc_feature as feature_gate;
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId};
use rustc_metadata;
use rustc_metadata::{creader::CrateLoader, creader::CStore, };
use rustc_incremental;
use rustc_resolve::Resolver;
use rustc_span::DUMMY_SP;
use rustc_span::symbol::{Symbol, };
use crossbeam_utils::sync::WaitGroup;
use parking_lot::RwLock;
use tempfile::{Builder as TDBuilder, };
use crate::{AcceleratorTargetDesc, context::Context, };
use crate::utils::{HashMap, StableHash, };
use self::error::IntoErrorWithKernelInstance;
pub use self::driver_data::DriverData;
mod collector;
pub mod error;
mod driver_data;
mod util;
use super::{PlatformCodegen, PKernelDesc, };
use super::products::*;
use crate::codegen::worker::error::PError;
use crate::metadata::{CrateMetadataLoader, CrateMetadata, CrateNameHash, DummyMetadataLoader};
use crate::utils::env::{use_llc, print_opt_remarks};
const CRATE_NAME: &'static str = "geobacter-cross-codegen";
// TODO we need to create a talk to a "host codegen" so that we can ensure
// that adt's have the same layout in the shader/kernel as on the host.
// TODO XXX only one codegen query is allowed at a time.
// TODO codegen worker workers (ie codegen multiple functions concurrently)
// Note: recreate the session/tyctxt on *every* codegen. It is not safe to reuse.
static SETUP_RUSTC_INTERFACE_CALLBACKS: Once = Once::new();
pub struct CodegenDriver<P>(WorkerTranslatorData<P>)
where P: PlatformCodegen;
impl<P> CodegenDriver<P>
where P: PlatformCodegen,
{
pub fn new(context: &Context,
accel_desc: Arc<AcceleratorTargetDesc>,
platform: P)
-> io::Result<Self>
{
SETUP_RUSTC_INTERFACE_CALLBACKS.call_once(|| {
rustc_interface::callbacks::setup_callbacks();
});
let inner = WorkerTranslatorData {
context: context.clone(),
platform,
target_desc: accel_desc,
accels: Default::default(),
cache: Default::default(),
};
Ok(CodegenDriver(inner))
}
pub fn codegen(&self, desc: PKernelDesc<P>)
-> Result<Arc<PCodegenResults<P>>, error::PError<P>>
{
self.0.codegen_kernel(desc)
}
pub fn add_accel(&self, accel: &Arc<P::Device>) {
let mut this = self.0.accels.write();
this.push(Arc::downgrade(accel));
}
}
#[allow(dead_code)]
pub(crate) enum Message<P>
where P: PlatformCodegen,
{
/// So we can know when to exit.
AddAccel(Weak<P::Device>),
Codegen {
desc: PKernelDesc<P>,
ret: Sender<Result<Arc<PCodegenResults<P>>, error::PError<P>>>,
},
}
type IntrinsicsMap = FxHashMap<Symbol, Lrc<dyn CustomIntrinsicMirGen>>;
enum MaybeInProgress<P>
where P: PlatformCodegen,
{
InProgress(WaitGroup),
Done(Arc<PCodegenResults<P>>),
}
impl<P> Clone for MaybeInProgress<P>
where P: PlatformCodegen,
{
fn clone(&self) -> Self {
match self {
MaybeInProgress::InProgress(p) => MaybeInProgress::InProgress(p.clone()),
MaybeInProgress::Done(r) => MaybeInProgress::Done(r.clone()),
}
}
}
pub struct WorkerTranslatorData<P>
where P: PlatformCodegen,
{
pub(self) context: Context,
pub(crate) platform: P,
pub target_desc: Arc<AcceleratorTargetDesc>,
pub accels: RwLock<Vec<Weak<P::Device>>>,
cache: RwLock<HashMap<PKernelDesc<P>, MaybeInProgress<P>>>,
}
impl<P> WorkerTranslatorData<P>
where P: PlatformCodegen,
{
pub fn new(ctx: &Context,
target_desc: Arc<AcceleratorTargetDesc>,
platform: P)
-> io::Result<CodegenDriver<P>>
{
use std::thread::Builder;
let (_tx, rx) = channel();
let context = ctx.clone();
let name = format!("codegen thread for {}",
target_desc.target.llvm_target);
let f = move || {
info!("codegen thread for {} startup",
target_desc.target.llvm_target);
let mut data = WorkerTranslatorData {
context,
platform,
target_desc,
accels: Default::default(),
cache: Default::default(),
};
data.thread(&rx);
};
let _ = Builder::new()
.name(name)
.spawn(f)?;
unimplemented!("TODO: host codegen queries");
}
/// Only used for host codegen queries.
fn thread(&mut self, rx: &Receiver<Message<P>>) {
enum InternalMessage<D> {
Timeout,
AddAccel(Weak<D>),
}
let to = Duration::from_secs(10);
let mut recv_msg = None;
// Ensure we don't timeout and quit before we've received the
// first message (which should be an add accel message)
let mut first_msg = true;
'outer: loop {
let internal_msg = 'inner: loop {
let msg = recv_msg
.take()
.unwrap_or_else(|| {
rx.recv_timeout(to)
});
let msg = match msg {
Ok(msg) => msg,
Err(RecvTimeoutError::Timeout) => {
break 'inner InternalMessage::Timeout;
},
Err(error) => {
warn!("receive error: {:?}", error);
return;
},
};
match msg {
Message::AddAccel(accel) => {
first_msg = false;
break 'inner InternalMessage::AddAccel(accel);
},
_ if first_msg => {
panic!("first message must be Message::AddAccel; \
fix your Accelerator impl");
},
Message::Codegen {
desc,
ret,
} => {
let result = self.codegen_kernel(desc);
let _ = ret.send(result);
},
}
};
match internal_msg {
InternalMessage::Timeout => { },
InternalMessage::AddAccel(accel) => {
self.accels.write().push(accel);
continue 'outer;
},
}
let live = {
let accels = self.accels.read();
accels.iter()
.any(|a| a.upgrade().is_some())
};
if !live && !first_msg { return; }
// else wait for the next message, at which point we will reinitialize.
match rx.recv() {
Err(error) => {
warn!("receive error: {:?}", error);
return;
},
Ok(msg) => {
recv_msg = Some(Ok(msg));
},
}
}
}
fn initialize_sess<F, R>(&self, f: F) -> Result<R, error::PError<P>>
where F: FnOnce(Session, CStore) -> Result<R, error::PError<P>> + Send,
R: Send,
{
use rustc_data_structures::rayon::*;
use rustc_session::{DiagnosticOutput, Limit};
let f = move || {
let metadata = self.context.load_metadata()
.map_err(error::Error::LoadMetadata)?;
let mut opts = create_rustc_options();
self.platform.modify_rustc_session_options(&self.target_desc,
&mut opts);
let registry = rustc_driver::diagnostics_registry();
let diag = DiagnosticOutput::Default;
let lint_caps = Default::default();
let mut sess = rustc_session::build_session(opts, None, registry,
diag, lint_caps, None, None);
sess.init_crate_types(sess.opts.crate_types.clone());
sess.recursion_limit.set(Limit::new(512)).unwrap();
sess.type_length_limit.set(Limit::new(1048576)).unwrap();
sess.const_eval_limit.set(Limit::new(1_000_000)).unwrap();
sess.init_features(feature_gate::Features::default());
// TODO hash the accelerator target desc
let dis = self::util::compute_crate_disambiguator(&sess);
sess.crate_disambiguator.set(dis).unwrap();
self.target_desc.rustc_target_options(&mut sess.target.target);
// initialize the cstore for this codegen:
// We have to do this everytime because the CStore does some
// behind the scenes (vs letting the query system do it) caching
// which causes missing allocations the second time around.
// XXX fix upstream to remove that implicit assumption, that is
// 1 cstore per 1 tcx (1 to 1 and onto).
let mut cstore = CStore::default();
{
let mut loader = CrateMetadataLoader::default();
let CrateMetadata(meta) = loader.build(&metadata, &mut cstore)
.map_err(|err| error::Error::LoadMetadata(err.into()) )?;
for meta in meta.into_iter() {
let name = CrateNameHash {
name: meta.root.name(),
hash: meta.root.hash().as_u64(),
};
let cnum = loader.lookup_cnum(&name)
.unwrap();
cstore.set_crate_data(cnum, meta);
}
}
f(sess, cstore)
};
if current_thread_index().is_none() {
self.context.with_rustc_span_globals(f)
} else {
f()
}
}
fn codegen_kernel(&self, desc: PKernelDesc<P>)
-> Result<Arc<PCodegenResults<P>>, error::PError<P>>
{
use std::collections::hash_map::Entry;
loop {
loop {
let cache = self.cache.read();
if let Some(results) = cache.get(&desc).cloned() {
// we *MUST* drop the read lock before waiting on in progress codegen.
drop(cache);
let progress = match results {
MaybeInProgress::InProgress(progress) => progress,
MaybeInProgress::Done(results) => {
return Ok(results);
},
};
debug!("{:?}: waiting for existing codegen..", desc.instance);
progress.wait();
continue;
} else {
break;
}
}
let mut cache = self.cache.write();
match cache.entry(desc.clone()) {
Entry::Occupied(_) => {
// someone beat us.
continue;
},
Entry::Vacant(v) => {
v.insert(MaybeInProgress::InProgress(WaitGroup::new()));
break;
},
}
}
let result = self.initialize_sess(|sess, cstore, | {
self.codegen_kernel_inner(desc.clone(),
sess,
cstore)
.map(Arc::new)
});
let mut cache = self.cache.write();
match result {
Ok(ref result) => {
let entry = cache.get_mut(&desc).unwrap();
*entry = MaybeInProgress::Done(result.clone());
},
Err(_) => {
// we still need to unblock other threads
cache.remove(&desc);
},
}
result
}
fn codegen_kernel_inner(&self,
desc: PKernelDesc<P>,
sess: Session,
cstore: CStore)
-> Result<PCodegenResults<P>, error::PError<P>>
{
use self::util::get_codegen_backend;
use rustc_hir::definitions::Definitions;
let context = &self.context;
let instance = desc.instance;
let hash = instance.stable_hash();
info!("translating {:?}, hash: 0x{:x}",
instance, hash);
let codegen = get_codegen_backend(&sess.opts);
codegen.init(&sess);
// extern only providers:
let mut local_providers = rustc_middle::ty::query::Providers::default();
self::util::default_provide(&mut local_providers);
codegen.provide(&mut local_providers);
Self::providers_local(&mut local_providers);
let mut extern_providers = local_providers.clone();
self::util::default_provide_extern(&mut extern_providers);
codegen.provide_extern(&mut extern_providers);
Self::provide_extern_overrides(&mut extern_providers);
let disk_cache = rustc_incremental::load_query_result_cache(&sess);
let tmpdir = TDBuilder::new()
.prefix("geobacter-runtime-codegen-")
.tempdir()
.with_kernel_instance(desc.instance)?;
let out = OutputFilenames::new(
tmpdir.path().into(),
"codegen.elf".into(),
None,
Default::default(),
sess.opts.output_types.clone(),
);
let krate = create_empty_hir_crate();
let dep_graph = rustc_middle::dep_graph::DepGraph::new(Default::default(),
Default::default());
let arenas = WorkerLocal::new(|_| Arena::default());
let ast_krate = ast::Crate {
module: ast::Mod {
inner: DUMMY_SP,
items: vec![],
inline: false,
unsafety: ast::Unsafe::No,
},
attrs: vec![],
span: DUMMY_SP,
proc_macros: Default::default(),
};
let resolver_arenas = Resolver::arenas();
let crate_name = CRATE_NAME;
let crate_loader = CrateLoader::new_from_cstore(&sess, &DummyMetadataLoader,
crate_name, cstore);
let resolver = Resolver::new_with_cloader(&sess, &ast_krate, crate_name,
&resolver_arenas, crate_loader);
let mut resolutions = resolver.into_outputs();
let definitions: &Definitions = arenas.alloc(mem::replace(
&mut resolutions.definitions,
Definitions::new(crate_name, sess.local_crate_disambiguator()),
));
let mut intrinsics = IntrinsicsMap::default();
{
rustc_geobacter::intrinsics::insert_generic_intrinsics(|k, v| {
let k = Symbol::intern(&k);
assert!(intrinsics.insert(k, v).is_none());
});
}
{
let mut inserter = |k: &str, v: Lrc<dyn CustomIntrinsicMirGen + 'static>| {
let k = Symbol::intern(k);
assert!(intrinsics.insert(k, v).is_none());
};
self.platform
.insert_intrinsics(&self.target_desc, &mut inserter);
self.platform
.insert_kernel_intrinsics(&desc, &mut inserter);
}
let accels = self.accels.read().clone();
let driver_data: DriverData<P> =
DriverData::new(context.clone(),
&accels,
&self.target_desc,
intrinsics,
&self.platform);
let driver_data: DriverData<'static, P> = unsafe {
::std::mem::transmute(driver_data)
};
let driver_data = Box::new(driver_data) as Box<dyn Any + Send + Sync>;
let gcx = TyCtxt::create_global_ctxt(
&sess,
Lrc::new(rustc_lint::LintStore::new()),
local_providers,
extern_providers,
&arenas,
resolutions,
&krate,
&definitions,
dep_graph.clone(),
disk_cache,
CRATE_NAME,
&out,
Some(driver_data),
);
let icx = ty::tls::ImplicitCtxt::new(&gcx);
let results: Result<PCodegenResults<P>, PError<P>> = ty::tls::enter_context(&icx, |icx| {
let tcx = icx.tcx;
// Do some initialization of the DepGraph that can only be done with the
// tcx available.
tcx.sess.time("dep graph tcx init", || rustc_incremental::dep_graph_tcx_init(tcx));
DriverData::<P>::with(tcx, |tcx, pd| -> Result<(), PError<P>> {
use rustc_geobacter::TyCtxtKernelInstance;
unsafe {
let spec_data = &mut *pd.spec_data.get();
for (&k, v) in desc.spec_params.iter() {
let instance = tcx.convert_kernel_instance(k)
.ok_or_else(|| error::Error::ConvertKernelInstance(k))?;
spec_data.insert(instance, v.clone());
}
}
Ok(())
})?;
tcx.sess.time("platform root and condition init",
move || {
DriverData::<P>::with(tcx, |tcx, pd| {
pd.init_root(desc, tcx)?;
pd.init_conditions(tcx)?;
pd.pre_codegen(tcx)
})
})?;
let metadata = EncodedMetadata::new();
let need_metadata_module = false;
let ongoing_codegen = tcx.sess.time("codegen", || {
let _prof_timer = tcx.prof.generic_activity("codegen_crate");
codegen.codegen_crate(tcx, metadata, need_metadata_module)
});
let codegen_results = tcx.sess.time("LLVM codegen",
|| {
codegen.join_codegen(ongoing_codegen, &sess, &dep_graph)
.map_err(|_| {
error::Error::Codegen
})
})?;
tcx.sess.time("link",
|| {
codegen.link(&sess, codegen_results, &out)
.map_err(|_| {
error::Error::Linking
})
})?;
let results = DriverData::<P>::with(tcx, |tcx, pd| {
pd.post_codegen(tcx, &tmpdir.path(), &out)
})?;
Ok(results)
});
let mut results = results?;
let output_dir = tmpdir.into_path();
self.platform
.post_codegen(&self.target_desc,
&output_dir,
&mut results)
.map_err(error::Error::PostCodegen)?;
// check that the platform actually inserted an exe entry:
debug_assert!(results.exe_ref().is_some(),
"internal platform codegen error: platform didn't insert an Exe \
output type into the results");
info!("codegen intermediates dir: {}", output_dir.display());
info!("codegen complete {:?}, hash: 0x{:x}",
instance, hash);
Ok(results)
}
}
pub fn create_rustc_options() -> rustc_session::config::Options {
use rustc_session::config::*;
use rustc_target::spec::*;
let mut opts = Options::default();
// We need to have the tcx build the def_path_hash_to_def_id map:
opts.debugging_opts.query_dep_graph = true;
opts.optimize = OptLevel::No;
opts.optimize = OptLevel::Aggressive;
//opts.debuginfo = DebugInfo::Limited;
let output = (OutputType::Bitcode, None);
let ir_out = (OutputType::LlvmAssembly, None);
let mut out = Vec::new();
out.push(output);
out.push(ir_out);
if opts.optimize == OptLevel::Aggressive && !use_llc() {
// prevent LLVM from taking us down if we're not optimizing:
let asm = (OutputType::Assembly, None);
let obj = (OutputType::Object, None);
out.push(asm);
out.push(obj);
}
opts.output_types = OutputTypes::new(&out);
let print_remarks = print_opt_remarks();
if print_remarks {
opts.debuginfo = DebugInfo::Limited;
}
// Avoid a mystery warning about not being about to remove a temp
// object after codegen.
opts.cg.save_temps = true;
opts.cg.lto = LtoCli::No;
opts.cg.panic = Some(PanicStrategy::Abort);
opts.cg.incremental = None;
opts.cg.overflow_checks = Some(false);
if print_remarks {
opts.cg.remark = Passes::All;
}
opts.cli_forced_codegen_units = Some(1);
opts.incremental = None;
opts.debugging_opts.verify_llvm_ir = false;
opts.cg.no_prepopulate_passes = false;
if opts.cg.no_prepopulate_passes {
opts.cg.passes.push("name-anon-globals".into());
} else if opts.optimize != OptLevel::No {
// Should we run this unconditionally?
opts.cg.passes.push("wholeprogramdevirt".into());
opts.cg.passes.push("speculative-execution".into());
for _ in 0..2 {
opts.cg.passes.push("infer-address-spaces".into());
opts.cg.passes.push("instcombine".into());
opts.cg.passes.push("sroa".into());
}
opts.cg.passes.push("simplifycfg".into());
}
opts.debugging_opts.print_llvm_passes = false;
opts.cg.llvm_args.push("-spec-exec-only-if-divergent-target".into());
opts.cg.llvm_args.push("-amdgpu-early-inline-all".into());
opts.cg.llvm_args.push("-amdgpu-prelink".into());
opts.cg.llvm_args.push("-sroa-strict-inbounds".into());
opts.debugging_opts.polly =
opts.optimize == OptLevel::Aggressive;
opts.cg.llvm_args.push("-polly-run-inliner".into());
opts.cg.llvm_args.push("-polly-register-tiling".into());
opts.cg.llvm_args.push("-polly-check-vectorizable".into());
opts.cg.llvm_args.push("-enable-polly-aligned".into());
opts.cg.llvm_args.push("-polly-vectorizer=stripmine".into());
//opts.cg.llvm_args.push("-polly-position=early".into());
opts.cg.llvm_args.push("-polly-enable-polyhedralinfo".into());
// Disable these alignment assumptions inserted during optimization:
// They aren't really helpful and in fact block a lot of possible mem2reg promotions as a
// result of their presence.
opts.cg.llvm_args
.push("-preserve-alignment-assumptions-during-inlining=false".into());
opts
}
pub fn create_empty_hir_crate<'hir>() -> rustc_hir::Crate<'hir> {
use rustc_hir::*;
let m = Mod {
inner: DUMMY_SP,
item_ids: Default::default(),
};
let attrs = Default::default();
let span = DUMMY_SP;
let exported_macros = Default::default();
let items = BTreeMap::new();
let trait_items = BTreeMap::new();
let impl_items = BTreeMap::new();
let bodies = BTreeMap::new();
let trait_impls = BTreeMap::new();
let modules = BTreeMap::new();
let body_ids = Vec::new();
Crate {
item: CrateItem {
module: m,
attrs,
span,
},
exported_macros,
items,
trait_items,
impl_items,
bodies,
trait_impls,
body_ids,
modules,
non_exported_macro_attrs: Default::default(),
proc_macros: Default::default(),
trait_map: Default::default(),
}
}
impl<P> WorkerTranslatorData<P>
where P: PlatformCodegen,
{
pub fn provide_mir_overrides(providers: &mut Providers) {
*providers = Providers {
is_mir_available: |tcx, def_id| {
let mut providers = Providers::default();
rustc_metadata::provide_extern(&mut providers);
let def_id = DriverData::<P>::with(tcx, |tcx, pd| {
pd.stubber.stub_def_id(tcx, def_id)
});
(providers.is_mir_available)(tcx, def_id)
},
optimized_mir: |tcx, def_id| {
let mut providers = Providers::default();
rustc_metadata::provide_extern(&mut providers);
let def_id = DriverData::<P>::with(tcx, |tcx, pd| {
pd.stubber.stub_def_id(tcx, def_id)
});
(providers.optimized_mir)(tcx, def_id)
},
symbol_name: |tcx, instance| {
let mut providers = Providers::default();
rustc_symbol_mangling::provide(&mut providers);
let instance = tcx.stubbed_instance(instance);
(providers.symbol_name)(tcx, instance)
},
..*providers
}
}
pub fn provide_extern_overrides(providers: &mut Providers) {
Self::provide_mir_overrides(providers);
Self::providers_remote_and_local(providers);
}
pub fn providers_local(providers: &mut Providers) {
use rustc_data_structures::svh::Svh;
use rustc_hir::def_id::{LOCAL_CRATE};
*providers = Providers {
crate_hash: |_tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
// XXX?
Svh::new(1)
},
crate_name: |_tcx, id| {
assert_eq!(id, LOCAL_CRATE);
Symbol::intern(CRATE_NAME)
},
crate_disambiguator: |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
tcx.sess.local_crate_disambiguator()
},
native_libraries: |_tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
Lrc::new(vec![])
},
link_args: |_tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
Lrc::new(vec![])
},
type_of: |tcx, def_id| {
DriverData::<P>::with(tcx, |_tcx, pd| {
pd.expect_type_of(def_id)
})
},
dependency_formats: |tcx, cnum| {
use rustc_middle::middle::dependency_format::Linkage;
use rustc_session::config::CrateType;
assert_eq!(cnum, LOCAL_CRATE);
let fake = tcx.all_crate_nums(LOCAL_CRATE)
.iter()
.map(|_| Linkage::NotLinked )
.collect();
vec![(CrateType::Cdylib, fake)].into()
},
..*providers
};
Self::provide_mir_overrides(providers);
Self::providers_remote_and_local(providers);
}
fn providers_remote_and_local(providers: &mut Providers) {
use rustc_session::config::EntryFnType;
*providers = Providers {
fn_sig: |tcx, def_id| {
use rustc_middle::ty::{Binder, FnSig, };
let mut providers = Providers::default();
rustc_metadata::provide_extern(&mut providers);
// no stubbing here. We want the original fn sig
let sig = (providers.fn_sig)(tcx, def_id);
DriverData::<P>::with(tcx, |_tcx, pd| {
if pd.is_root(def_id) {
// modify the abi:
let sig = FnSig {
abi: pd.target_desc.kernel_abi.clone(),
..sig.skip_binder()
};
Binder::bind(sig)
} else {
sig
}
})
},
reachable_non_generics,
custom_intrinsic_mirgen: |tcx, def_id| {
DriverData::<P>::with(tcx, |tcx, pd| {
let name = tcx.item_name(def_id);
debug!("custom intrinsic: {}", name);
pd.intrinsics
.get(&name)
.cloned()
})
},
upstream_monomorphizations,
upstream_monomorphizations_for,
codegen_fn_attrs: |tcx, def_id| {
let mut providers = Providers::default();
rustc_typeck::provide(&mut providers);
let mut attrs = (providers.codegen_fn_attrs)(tcx, def_id);
DriverData::<P>::with(tcx, move |tcx, pd| {
pd.platform.codegen_fn_attrs(tcx, pd, def_id, &mut attrs);
if let Some(ref spirv) = attrs.spirv {
info!("spirv attrs for {:?}: {:#?}", def_id, spirv);
}
attrs
})
},
item_attrs: |tcx, def_id| {
let mut providers = Providers::default();
rustc_metadata::provide_extern(&mut providers);
// Note: no replace here. For one, we'll introduce a cycle. And
// we don't want to use the attributes of a different item anyway.
let attrs = (providers.item_attrs)(tcx, def_id);
DriverData::<P>::with(tcx, move |tcx, pd| {
pd.platform.item_attrs(tcx, pd, def_id, attrs)
})
},
entry_fn,
collect_and_partition_mono_items: |tcx, cnum| {
DriverData::<P>::with(tcx, move |tcx, pd| {
collector::collect_and_partition_mono_items(tcx, pd, cnum)
})
},
// we need to override this because otherwise rustc will get confused
// which will eventually lead to an out of bounds slice index.
// our synthetic crate will never have any `extern crate ...;`, so
// overriding this should be a bit of an optimization anyway in
// addition to being a bugfix.
missing_extern_crate_item: |_tcx, _cnum| { true },
specialization_data: |tcx, inst| {
DriverData::<P>::with(tcx, move |_, pd| {
pd.spec_data()
.get(&inst)
.map(|v| &v[..] )
})
},
stubbed_instance: |tcx, inst| {
match inst.def {
ty::InstanceDef::Item(def) => {
if def.as_const_arg().is_some() { return inst; }
},
_ => { return inst; },
};
DriverData::<P>::with(tcx, move |tcx, pd| {
let did = pd.stubber.stub_def_id(tcx, inst.def_id());
// we should be able to just replace `stub_instance.substs`
// with `inst.substs`, assuming our stub signatures match the
// real functions (which they should).
let def = ty::WithOptConstParam::unknown(did);
ty::Instance {
def: ty::InstanceDef::Item(def),
substs: inst.substs,
}
})
},
..*providers
};
fn entry_fn<'tcx>(_tcx: TyCtxt<'tcx>, _cnum: CrateNum) -> Option<(LocalDefId, EntryFnType)> {
None
}
fn reachable_non_generics<'tcx>(_tcx: TyCtxt<'tcx>, _cnum: CrateNum)
-> DefIdMap<SymbolExportLevel>
{
// we need to recodegen everything
Default::default()
}
fn upstream_monomorphizations(_tcx: TyCtxt, _cnum: CrateNum)
-> DefIdMap<FxHashMap<SubstsRef, CrateNum>>
{
// we never have any upstream monomorphizations.
Default::default()
}
fn upstream_monomorphizations_for(_tcx: TyCtxt, _def_id: DefId)
-> Option<&FxHashMap<SubstsRef, CrateNum>>
{
None
}
}
}
pub struct TyCtxtLessKernelId {
pub crate_name: String,
pub crate_hash_hi: u64,
pub crate_hash_lo: u64,
pub index: u64,
}
impl TyCtxtLessKernelId {
pub fn from_def_id(tcx: TyCtxt<'_>,
def_id: DefId) -> Self {
let crate_name = tcx.crate_name(def_id.krate);
let crate_name = format!("{}", crate_name);
let disambiguator = tcx.crate_disambiguator(def_id.krate);
let (crate_hash_hi, crate_hash_lo) = disambiguator.to_fingerprint().as_value();
let index = def_id.index.as_usize() as u64;
TyCtxtLessKernelId {
crate_name,
crate_hash_hi,
crate_hash_lo,
index,
}
}
}