-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrustc-nvptx-ptrcast-fix.patch
922 lines (861 loc) · 38.5 KB
/
rustc-nvptx-ptrcast-fix.patch
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
From 1f39fa119fcc04453f4eeb80013392e2a740e0a5 Mon Sep 17 00:00:00 2001
From: Julia Tatz <[email protected]>
Date: Sun, 3 Jul 2022 13:48:14 -0400
Subject: [PATCH 1/5] Can't bitcast pointers in diffrent address spaces Use
pointercast instead After llvm transitions to opaque pointers, this will not
always be necessary Keeping asserts for now, but need to be removed
---
compiler/rustc_codegen_llvm/src/builder.rs | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)
diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs
index 7b259055d40..4d9bb511cde 100644
--- a/compiler/rustc_codegen_llvm/src/builder.rs
+++ b/compiler/rustc_codegen_llvm/src/builder.rs
@@ -831,6 +831,14 @@ fn inttoptr(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
}
fn bitcast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
+ // FIXME: remove before merge
+ assert!(
+ !(matches!(self.cx.type_kind(self.cx.val_ty(val)), TypeKind::Pointer)
+ || matches!(self.cx.type_kind(dest_ty), TypeKind::Pointer)),
+ "val ty is {:?}, dest_ty is {:?}",
+ self.cx.type_kind(self.cx.val_ty(val)),
+ self.cx.type_kind(dest_ty),
+ );
unsafe { llvm::LLVMBuildBitCast(self.llbuilder, val, dest_ty, UNNAMED) }
}
@@ -847,6 +855,14 @@ fn intcast(&mut self, val: &'ll Value, dest_ty: &'ll Type, is_signed: bool) -> &
}
fn pointercast(&mut self, val: &'ll Value, dest_ty: &'ll Type) -> &'ll Value {
+ // FIXME: remove before merge
+ assert!(
+ (matches!(self.cx.type_kind(self.cx.val_ty(val)), TypeKind::Pointer)
+ && matches!(self.cx.type_kind(dest_ty), TypeKind::Pointer)),
+ "val ty is {:?}, dest_ty is {:?}",
+ self.cx.type_kind(self.cx.val_ty(val)),
+ self.cx.type_kind(dest_ty),
+ );
unsafe { llvm::LLVMBuildPointerCast(self.llbuilder, val, dest_ty, UNNAMED) }
}
@@ -1421,7 +1437,11 @@ fn check_call<'b>(
Expected {:?} for param {}, got {:?}; injecting bitcast",
llfn, expected_ty, i, actual_ty
);
- self.bitcast(actual_val, expected_ty)
+ if let TypeKind::Pointer = self.cx.type_kind(expected_ty) {
+ self.pointercast(actual_val, expected_ty)
+ } else {
+ self.bitcast(actual_val, expected_ty)
+ }
} else {
actual_val
}
--
2.42.0
From e70af4616c288521dff7fc58cb17a5b1325947a4 Mon Sep 17 00:00:00 2001
From: Julia Tatz <[email protected]>
Date: Mon, 12 Sep 2022 17:45:02 -0400
Subject: [PATCH 2/5] Fix const ptr casts too
---
compiler/rustc_codegen_llvm/src/common.rs | 4 ++--
compiler/rustc_codegen_llvm/src/consts.rs | 24 +++++++++++++++++++++++
2 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/compiler/rustc_codegen_llvm/src/common.rs b/compiler/rustc_codegen_llvm/src/common.rs
index 0b0816c27b6..fc12ece7eee 100644
--- a/compiler/rustc_codegen_llvm/src/common.rs
+++ b/compiler/rustc_codegen_llvm/src/common.rs
@@ -286,7 +286,7 @@ fn scalar_to_backend(&self, cv: Scalar, layout: abi::Scalar, llty: &'ll Type) ->
let llval = unsafe {
llvm::LLVMConstInBoundsGEP2(
self.type_i8(),
- self.const_bitcast(base_addr, self.type_ptr_ext(base_addr_space)),
+ self.const_ptrcast(base_addr, self.type_ptr_ext(base_addr_space)),
&self.const_usize(offset.bytes()),
1,
)
@@ -294,7 +294,7 @@ fn scalar_to_backend(&self, cv: Scalar, layout: abi::Scalar, llty: &'ll Type) ->
if !matches!(layout.primitive(), Pointer(_)) {
unsafe { llvm::LLVMConstPtrToInt(llval, llty) }
} else {
- self.const_bitcast(llval, llty)
+ self.const_ptrcast(llval, llty)
}
}
}
diff --git a/compiler/rustc_codegen_llvm/src/consts.rs b/compiler/rustc_codegen_llvm/src/consts.rs
index 73821b1685d..6cb048472be 100644
--- a/compiler/rustc_codegen_llvm/src/consts.rs
+++ b/compiler/rustc_codegen_llvm/src/consts.rs
@@ -202,9 +202,33 @@ fn check_and_apply_linkage<'ll, 'tcx>(
impl<'ll> CodegenCx<'ll, '_> {
pub(crate) fn const_bitcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
+ // FIXME: remove before merge
+ assert!(
+ !(matches!(
+ self.type_kind(self.val_ty(val)),
+ rustc_codegen_ssa::common::TypeKind::Pointer
+ ) || matches!(self.type_kind(ty), rustc_codegen_ssa::common::TypeKind::Pointer)),
+ "val ty is {:?}, dest_ty is {:?}",
+ self.type_kind(self.val_ty(val)),
+ self.type_kind(ty),
+ );
unsafe { llvm::LLVMConstBitCast(val, ty) }
}
+ pub(crate) fn const_ptrcast(&self, val: &'ll Value, ty: &'ll Type) -> &'ll Value {
+ // FIXME: remove before merge
+ assert!(
+ (matches!(
+ self.type_kind(self.val_ty(val)),
+ rustc_codegen_ssa::common::TypeKind::Pointer
+ ) && matches!(self.type_kind(ty), rustc_codegen_ssa::common::TypeKind::Pointer)),
+ "val ty is {:?}, dest_ty is {:?}",
+ self.type_kind(self.val_ty(val)),
+ self.type_kind(ty),
+ );
+ unsafe { llvm::LLVMConstPointerCast(val, ty) }
+ }
+
pub(crate) fn static_addr_of_mut(
&self,
cv: &'ll Value,
--
2.42.0
From 7fb97a29c33684edb84778ebbaa6d7b274c1a4de Mon Sep 17 00:00:00 2001
From: Julia Tatz <[email protected]>
Date: Mon, 9 Jan 2023 20:51:01 -0500
Subject: [PATCH 3/5] Add new experimental #[address_space] built-in
Only the skeleton for now, no codegen yet
---
.../rustc_codegen_ssa/src/codegen_attrs.rs | 45 +++++++++++++++++++
compiler/rustc_feature/src/builtin_attrs.rs | 1 +
compiler/rustc_feature/src/unstable.rs | 2 +
.../src/middle/codegen_fn_attrs.rs | 4 ++
compiler/rustc_span/src/symbol.rs | 1 +
.../feature-gate-address_space.rs | 13 ++++++
.../feature-gate-address_space.stderr | 38 ++++++++++++++++
7 files changed, 104 insertions(+)
create mode 100644 tests/ui/feature-gates/feature-gate-address_space.rs
create mode 100644 tests/ui/feature-gates/feature-gate-address_space.stderr
diff --git a/compiler/rustc_codegen_ssa/src/codegen_attrs.rs b/compiler/rustc_codegen_ssa/src/codegen_attrs.rs
index 2e0840f2d1b..e43bce2b6e7 100644
--- a/compiler/rustc_codegen_ssa/src/codegen_attrs.rs
+++ b/compiler/rustc_codegen_ssa/src/codegen_attrs.rs
@@ -349,6 +349,11 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
codegen_fn_attrs.link_ordinal = ordinal;
}
}
+ sym::address_space => {
+ if let address_space @ Some(_) = check_address_space(tcx, attr) {
+ codegen_fn_attrs.address_space = address_space;
+ }
+ }
sym::no_sanitize => {
no_sanitize_span = Some(attr.span);
if let Some(list) = attr.meta_item_list() {
@@ -697,6 +702,46 @@ fn check_link_name_xor_ordinal(
}
}
+fn check_address_space(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option<u16> {
+ use rustc_ast::{LitIntType, LitKind, MetaItemLit};
+ let meta_item_list = attr.meta_item_list();
+ let meta_item_list = meta_item_list.as_deref();
+ let sole_meta_list = match meta_item_list {
+ Some([item]) => item.lit(),
+ Some(_) => {
+ let msg = "incorrect number of arguments to `#[address_space]`";
+ tcx.sess
+ .struct_span_err(attr.span, msg)
+ .note("the attribute requires exactly one argument")
+ .emit();
+ return None;
+ }
+ _ => None,
+ };
+ if let Some(MetaItemLit { kind: LitKind::Int(ordinal, LitIntType::Unsuffixed), .. }) =
+ sole_meta_list
+ {
+ if *ordinal <= u16::MAX as u128 {
+ Some(*ordinal as u16)
+ } else {
+ let msg =
+ format!("address space value in `address_space` is too large: `{}`", &ordinal);
+ tcx.sess
+ .struct_span_err(attr.span, msg)
+ .note("the value may not exceed `u16::MAX`")
+ .emit();
+ None
+ }
+ } else {
+ let msg = "illegal address space format in `address_space`";
+ tcx.sess
+ .struct_span_err(attr.span, msg)
+ .note("an unsuffixed integer value, e.g., `1`, is expected")
+ .emit();
+ None
+ }
+}
+
pub fn provide(providers: &mut Providers) {
*providers = Providers { codegen_fn_attrs, should_inherit_track_caller, ..*providers };
}
diff --git a/compiler/rustc_feature/src/builtin_attrs.rs b/compiler/rustc_feature/src/builtin_attrs.rs
index e808e4815fe..0b2bbd5dbac 100644
--- a/compiler/rustc_feature/src/builtin_attrs.rs
+++ b/compiler/rustc_feature/src/builtin_attrs.rs
@@ -396,6 +396,7 @@ pub struct BuiltinAttribute {
experimental!(no_sanitize)
),
gated!(coverage, Normal, template!(Word, List: "on|off"), WarnFollowing, coverage_attribute, experimental!(coverage)),
+ gated!(address_space, Normal, template!(List: "space"), ErrorPreceding, experimental!(address_space)),
ungated!(
doc, Normal, template!(List: "hidden|inline|...", NameValueStr: "string"), DuplicatesOk
diff --git a/compiler/rustc_feature/src/unstable.rs b/compiler/rustc_feature/src/unstable.rs
index 695de54eefa..70debf35087 100644
--- a/compiler/rustc_feature/src/unstable.rs
+++ b/compiler/rustc_feature/src/unstable.rs
@@ -322,6 +322,8 @@ pub fn internal(&self, feature: Symbol) -> bool {
(unstable, abi_riscv_interrupt, "1.73.0", Some(111889), None),
/// Allows `extern "x86-interrupt" fn()`.
(unstable, abi_x86_interrupt, "1.17.0", Some(40180), None),
+ /// Allows `#[address_space = "..."]` on static varibles.
+ (incomplete, address_space, "CURRENT_RUSTC_VERSION", Some(1), None),
/// Allows additional const parameter types, such as `&'static str` or user defined types
(incomplete, adt_const_params, "1.56.0", Some(95174), None),
/// Allows defining an `#[alloc_error_handler]`.
diff --git a/compiler/rustc_middle/src/middle/codegen_fn_attrs.rs b/compiler/rustc_middle/src/middle/codegen_fn_attrs.rs
index f758c1d5e6f..770918d1936 100644
--- a/compiler/rustc_middle/src/middle/codegen_fn_attrs.rs
+++ b/compiler/rustc_middle/src/middle/codegen_fn_attrs.rs
@@ -43,6 +43,9 @@ pub struct CodegenFnAttrs {
/// The `#[repr(align(...))]` attribute. Indicates the value of which the function should be
/// aligned to.
pub alignment: Option<u32>,
+ /// The `#[address_space = "..."]` attribute, indicating in which address space a
+ /// Rust-defined/foreign static value resides.
+ pub address_space: Option<u16>,
}
bitflags! {
@@ -123,6 +126,7 @@ pub const fn new() -> CodegenFnAttrs {
no_sanitize: SanitizerSet::empty(),
instruction_set: None,
alignment: None,
+ address_space: None,
}
}
diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs
index 88d9dab2ba5..e0051f45721 100644
--- a/compiler/rustc_span/src/symbol.rs
+++ b/compiler/rustc_span/src/symbol.rs
@@ -360,6 +360,7 @@
add_assign,
add_with_overflow,
address,
+ address_space,
adt_const_params,
advanced_slice_patterns,
adx_target_feature,
diff --git a/tests/ui/feature-gates/feature-gate-address_space.rs b/tests/ui/feature-gates/feature-gate-address_space.rs
new file mode 100644
index 00000000000..886c5b564a6
--- /dev/null
+++ b/tests/ui/feature-gates/feature-gate-address_space.rs
@@ -0,0 +1,13 @@
+#[address_space(0)] //~ ERROR the `#[address_space]` attribute is an experimental feature
+static FOO: usize = 42;
+
+extern "C" {
+ #[address_space(0)] //~ ERROR the `#[address_space]` attribute is an experimental feature
+ static EXTERN_FOO: usize;
+}
+
+#[address_space(65_536)] //~ ERROR the `#[address_space]` attribute is an experimental feature
+ //~^ ERROR address space value in `address_space` is too large: `65536`
+static TOO_BIG: usize = 7;
+
+fn main() {}
diff --git a/tests/ui/feature-gates/feature-gate-address_space.stderr b/tests/ui/feature-gates/feature-gate-address_space.stderr
new file mode 100644
index 00000000000..91eedd877ff
--- /dev/null
+++ b/tests/ui/feature-gates/feature-gate-address_space.stderr
@@ -0,0 +1,38 @@
+error[E0658]: the `#[address_space]` attribute is an experimental feature
+ --> $DIR/feature-gate-address_space.rs:1:1
+ |
+LL | #[address_space(0)]
+ | ^^^^^^^^^^^^^^^^^^^
+ |
+ = note: see issue #1 <https://github.com/rust-lang/rust/issues/1> for more information
+ = help: add `#![feature(address_space)]` to the crate attributes to enable
+
+error[E0658]: the `#[address_space]` attribute is an experimental feature
+ --> $DIR/feature-gate-address_space.rs:5:5
+ |
+LL | #[address_space(0)]
+ | ^^^^^^^^^^^^^^^^^^^
+ |
+ = note: see issue #1 <https://github.com/rust-lang/rust/issues/1> for more information
+ = help: add `#![feature(address_space)]` to the crate attributes to enable
+
+error[E0658]: the `#[address_space]` attribute is an experimental feature
+ --> $DIR/feature-gate-address_space.rs:9:1
+ |
+LL | #[address_space(65_536)]
+ | ^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = note: see issue #1 <https://github.com/rust-lang/rust/issues/1> for more information
+ = help: add `#![feature(address_space)]` to the crate attributes to enable
+
+error: address space value in `address_space` is too large: `65536`
+ --> $DIR/feature-gate-address_space.rs:9:1
+ |
+LL | #[address_space(65_536)]
+ | ^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = note: the value may not exceed `u16::MAX`
+
+error: aborting due to 4 previous errors
+
+For more information about this error, try `rustc --explain E0658`.
--
2.42.0
From 90766bddb1f33ac9850f721dd28a80bd111cb974 Mon Sep 17 00:00:00 2001
From: Julia Tatz <[email protected]>
Date: Mon, 9 Jan 2023 21:01:37 -0500
Subject: [PATCH 4/5] Initial codegen for #[address_space], without tests yet
---
compiler/rustc_codegen_llvm/src/allocator.rs | 18 +++++++-
compiler/rustc_codegen_llvm/src/common.rs | 2 +-
compiler/rustc_codegen_llvm/src/consts.rs | 20 +++++----
compiler/rustc_codegen_llvm/src/context.rs | 2 +-
.../src/coverageinfo/mapgen.rs | 3 +-
.../src/coverageinfo/mod.rs | 5 ++-
.../rustc_codegen_llvm/src/debuginfo/gdb.rs | 2 +-
compiler/rustc_codegen_llvm/src/declare.rs | 38 ++++++++++++++---
compiler/rustc_codegen_llvm/src/intrinsic.rs | 4 +-
compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 15 ++++++-
compiler/rustc_codegen_llvm/src/llvm/mod.rs | 15 ++++++-
compiler/rustc_codegen_llvm/src/mono_item.rs | 2 +-
.../rustc_llvm/llvm-wrapper/RustWrapper.cpp | 41 ++++++++++++++++---
13 files changed, 134 insertions(+), 33 deletions(-)
diff --git a/compiler/rustc_codegen_llvm/src/allocator.rs b/compiler/rustc_codegen_llvm/src/allocator.rs
index db5c1388ef8..ce51fee67d3 100644
--- a/compiler/rustc_codegen_llvm/src/allocator.rs
+++ b/compiler/rustc_codegen_llvm/src/allocator.rs
@@ -75,7 +75,14 @@ pub(crate) unsafe fn codegen(
// __rust_alloc_error_handler_should_panic
let name = OomStrategy::SYMBOL;
- let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
+ let ll_g = llvm::LLVMRustGetOrInsertGlobal(
+ llmod,
+ name.as_ptr().cast(),
+ name.len(),
+ i8,
+ false,
+ 0 as c_uint,
+ );
if tcx.sess.target.default_hidden_visibility {
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);
}
@@ -84,7 +91,14 @@ pub(crate) unsafe fn codegen(
llvm::LLVMSetInitializer(ll_g, llval);
let name = NO_ALLOC_SHIM_IS_UNSTABLE;
- let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
+ let ll_g = llvm::LLVMRustGetOrInsertGlobal(
+ llmod,
+ name.as_ptr().cast(),
+ name.len(),
+ i8,
+ false,
+ 0 as c_uint,
+ );
if tcx.sess.target.default_hidden_visibility {
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);
}
diff --git a/compiler/rustc_codegen_llvm/src/common.rs b/compiler/rustc_codegen_llvm/src/common.rs
index fc12ece7eee..1413dc65b51 100644
--- a/compiler/rustc_codegen_llvm/src/common.rs
+++ b/compiler/rustc_codegen_llvm/src/common.rs
@@ -197,7 +197,7 @@ fn const_str(&self, s: &str) -> (&'ll Value, &'ll Value) {
.or_insert_with(|| {
let sc = self.const_bytes(s.as_bytes());
let sym = self.generate_local_symbol_name("str");
- let g = self.define_global(&sym, self.val_ty(sc)).unwrap_or_else(|| {
+ let g = self.define_global(&sym, self.val_ty(sc), None).unwrap_or_else(|| {
bug!("symbol `{}` is already defined", sym);
});
unsafe {
diff --git a/compiler/rustc_codegen_llvm/src/consts.rs b/compiler/rustc_codegen_llvm/src/consts.rs
index 6cb048472be..3574b62d2cf 100644
--- a/compiler/rustc_codegen_llvm/src/consts.rs
+++ b/compiler/rustc_codegen_llvm/src/consts.rs
@@ -161,7 +161,7 @@ fn check_and_apply_linkage<'ll, 'tcx>(
unsafe {
// Declare a symbol `foo` with the desired linkage.
- let g1 = cx.declare_global(sym, cx.type_i8());
+ let g1 = cx.declare_global(sym, cx.type_i8(), attrs.address_space);
llvm::LLVMRustSetLinkage(g1, base::linkage_to_llvm(linkage));
// Declare an internal global `extern_with_linkage_foo` which
@@ -172,7 +172,7 @@ fn check_and_apply_linkage<'ll, 'tcx>(
// zero.
let mut real_name = "_rust_extern_with_linkage_".to_string();
real_name.push_str(sym);
- let g2 = cx.define_global(&real_name, llty).unwrap_or_else(|| {
+ let g2 = cx.define_global(&real_name, llty, attrs.address_space).unwrap_or_else(|| {
cx.sess().emit_fatal(SymbolAlreadyDefined {
span: cx.tcx.def_span(def_id),
symbol_name: sym,
@@ -192,11 +192,12 @@ fn check_and_apply_linkage<'ll, 'tcx>(
true,
),
llty,
+ attrs.address_space
)
} else {
// Generate an external declaration.
// FIXME(nagisa): investigate whether it can be changed into define_global
- cx.declare_global(sym, llty)
+ cx.declare_global(sym, llty, attrs.address_space)
}
}
@@ -239,13 +240,14 @@ pub(crate) fn static_addr_of_mut(
let gv = match kind {
Some(kind) if !self.tcx.sess.fewer_names() => {
let name = self.generate_local_symbol_name(kind);
- let gv = self.define_global(&name, self.val_ty(cv)).unwrap_or_else(|| {
- bug!("symbol `{}` is already defined", name);
- });
+ let gv =
+ self.define_global(&name, self.val_ty(cv), None).unwrap_or_else(|| {
+ bug!("symbol `{}` is already defined", name);
+ });
llvm::LLVMRustSetLinkage(gv, llvm::Linkage::PrivateLinkage);
gv
}
- _ => self.define_private_global(self.val_ty(cv)),
+ _ => self.define_private_global(self.val_ty(cv), None),
};
llvm::LLVMSetInitializer(gv, cv);
set_global_alignment(self, gv, align);
@@ -282,7 +284,7 @@ pub(crate) fn get_static(&self, def_id: DefId) -> &'ll Value {
}
}
- let g = self.declare_global(sym, llty);
+ let g = self.declare_global(sym, llty, fn_attrs.address_space);
if !self.tcx.is_reachable_non_generic(def_id) {
unsafe {
@@ -427,6 +429,8 @@ fn codegen_static(&self, def_id: DefId, is_mutable: bool) {
name.as_ptr().cast(),
name.len(),
val_llty,
+ attrs.address_space.is_some(),
+ attrs.address_space.unwrap_or_default() as _,
);
llvm::LLVMRustSetLinkage(new_g, linkage);
diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs
index b4b2ab1e1f8..f096172331f 100644
--- a/compiler/rustc_codegen_llvm/src/context.rs
+++ b/compiler/rustc_codegen_llvm/src/context.rs
@@ -942,7 +942,7 @@ pub(crate) fn eh_catch_typeinfo(&self) -> &'ll Value {
Some(def_id) => self.get_static(def_id),
_ => {
let ty = self.type_struct(&[self.type_ptr(), self.type_ptr()], false);
- self.declare_global("rust_eh_catch_typeinfo", ty)
+ self.declare_global("rust_eh_catch_typeinfo", ty, None)
}
};
self.eh_catch_typeinfo.set(Some(eh_catch_typeinfo));
diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
index 274e0aeaaba..d4c216fa705 100644
--- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
+++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
@@ -133,7 +133,8 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
.collect::<Vec<_>>();
let initializer = cx.const_array(cx.type_ptr(), &name_globals);
- let array = llvm::add_global(cx.llmod, cx.val_ty(initializer), "__llvm_coverage_names");
+ let array =
+ llvm::add_global(cx.llmod, cx.val_ty(initializer), "__llvm_coverage_names", None);
llvm::set_global_constant(array, true);
llvm::set_linkage(array, llvm::Linkage::InternalLinkage);
llvm::set_initializer(array, initializer);
diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs
index 7d69756181a..11eea690f91 100644
--- a/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs
+++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs
@@ -219,7 +219,7 @@ pub(crate) fn save_cov_data_to_mod<'ll, 'tcx>(
.expect("Rust Coverage section name failed UTF-8 conversion");
debug!("covmap section name: {:?}", covmap_section_name);
- let llglobal = llvm::add_global(cx.llmod, cx.val_ty(cov_data_val), &covmap_var_name);
+ let llglobal = llvm::add_global(cx.llmod, cx.val_ty(cov_data_val), &covmap_var_name, None);
llvm::set_initializer(llglobal, cov_data_val);
llvm::set_global_constant(llglobal, true);
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
@@ -248,7 +248,8 @@ pub(crate) fn save_func_record_to_mod<'ll, 'tcx>(
debug!("function record var name: {:?}", func_record_var_name);
debug!("function record section name: {:?}", covfun_section_name);
- let llglobal = llvm::add_global(cx.llmod, cx.val_ty(func_record_val), &func_record_var_name);
+ let llglobal =
+ llvm::add_global(cx.llmod, cx.val_ty(func_record_val), &func_record_var_name, None);
llvm::set_initializer(llglobal, func_record_val);
llvm::set_global_constant(llglobal, true);
llvm::set_linkage(llglobal, llvm::Linkage::LinkOnceODRLinkage);
diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs b/compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs
index 425e935bc9f..9cf6f5f86f9 100644
--- a/compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs
+++ b/compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs
@@ -68,7 +68,7 @@ pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, '
let llvm_type = cx.type_array(cx.type_i8(), section_contents.len() as u64);
let section_var = cx
- .define_global(section_var_name, llvm_type)
+ .define_global(section_var_name, llvm_type, None)
.unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name));
llvm::LLVMSetSection(section_var, section_name.as_ptr().cast());
llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents));
diff --git a/compiler/rustc_codegen_llvm/src/declare.rs b/compiler/rustc_codegen_llvm/src/declare.rs
index 164b12cf8d4..c30a5ca04fa 100644
--- a/compiler/rustc_codegen_llvm/src/declare.rs
+++ b/compiler/rustc_codegen_llvm/src/declare.rs
@@ -65,9 +65,23 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
///
/// If there’s a value with the same name already declared, the function will
/// return its Value instead.
- pub fn declare_global(&self, name: &str, ty: &'ll Type) -> &'ll Value {
+ pub fn declare_global(
+ &self,
+ name: &str,
+ ty: &'ll Type,
+ address_space: Option<u16>,
+ ) -> &'ll Value {
debug!("declare_global(name={:?})", name);
- unsafe { llvm::LLVMRustGetOrInsertGlobal(self.llmod, name.as_ptr().cast(), name.len(), ty) }
+ unsafe {
+ llvm::LLVMRustGetOrInsertGlobal(
+ self.llmod,
+ name.as_ptr().cast(),
+ name.len(),
+ ty,
+ address_space.is_some(),
+ address_space.unwrap_or_default() as _,
+ )
+ }
}
/// Declare a C ABI function.
@@ -199,19 +213,31 @@ pub fn declare_fn(
/// return `None` if the name already has a definition associated with it. In that
/// case an error should be reported to the user, because it usually happens due
/// to user’s fault (e.g., misuse of `#[no_mangle]` or `#[export_name]` attributes).
- pub fn define_global(&self, name: &str, ty: &'ll Type) -> Option<&'ll Value> {
+ pub fn define_global(
+ &self,
+ name: &str,
+ ty: &'ll Type,
+ address_space: Option<u16>,
+ ) -> Option<&'ll Value> {
if self.get_defined_value(name).is_some() {
None
} else {
- Some(self.declare_global(name, ty))
+ Some(self.declare_global(name, ty, address_space))
}
}
/// Declare a private global
///
/// Use this function when you intend to define a global without a name.
- pub fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
- unsafe { llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty) }
+ pub fn define_private_global(&self, ty: &'ll Type, address_space: Option<u16>) -> &'ll Value {
+ unsafe {
+ llvm::LLVMRustInsertPrivateGlobal(
+ self.llmod,
+ ty,
+ address_space.is_some(),
+ address_space.unwrap_or_default() as _,
+ )
+ }
}
/// Gets declared value by name.
diff --git a/compiler/rustc_codegen_llvm/src/intrinsic.rs b/compiler/rustc_codegen_llvm/src/intrinsic.rs
index a97b803fc64..f9f81fb58be 100644
--- a/compiler/rustc_codegen_llvm/src/intrinsic.rs
+++ b/compiler/rustc_codegen_llvm/src/intrinsic.rs
@@ -569,11 +569,11 @@ fn codegen_msvc_try<'ll>(
//
// When modifying, make sure that the type_name string exactly matches
// the one used in library/panic_unwind/src/seh.rs.
- let type_info_vtable = bx.declare_global("??_7type_info@@6B@", bx.type_ptr());
+ let type_info_vtable = bx.declare_global("??_7type_info@@6B@", bx.type_ptr(), None);
let type_name = bx.const_bytes(b"rust_panic\0");
let type_info =
bx.const_struct(&[type_info_vtable, bx.const_null(bx.type_ptr()), type_name], false);
- let tydesc = bx.declare_global("__rust_panic_type_info", bx.val_ty(type_info));
+ let tydesc = bx.declare_global("__rust_panic_type_info", bx.val_ty(type_info), None);
unsafe {
llvm::LLVMRustSetLinkage(tydesc, llvm::Linkage::LinkOnceODRLinkage);
llvm::SetUniqueComdat(bx.llmod, tydesc);
diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs
index a038b3af03d..b4e2fe768a1 100644
--- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs
+++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs
@@ -991,14 +991,27 @@ pub fn LLVMConstInBoundsGEP2<'a>(
// Operations on global variables
pub fn LLVMIsAGlobalVariable(GlobalVar: &Value) -> Option<&Value>;
pub fn LLVMAddGlobal<'a>(M: &'a Module, Ty: &'a Type, Name: *const c_char) -> &'a Value;
+ pub fn LLVMAddGlobalInAddressSpace<'a>(
+ M: &'a Module,
+ Ty: &'a Type,
+ Name: *const c_char,
+ AddressSpace: c_uint,
+ ) -> &'a Value;
pub fn LLVMGetNamedGlobal(M: &Module, Name: *const c_char) -> Option<&Value>;
pub fn LLVMRustGetOrInsertGlobal<'a>(
M: &'a Module,
Name: *const c_char,
NameLen: size_t,
T: &'a Type,
+ InAddressSpace: bool,
+ AddressSpace: c_uint,
+ ) -> &'a Value;
+ pub fn LLVMRustInsertPrivateGlobal<'a>(
+ M: &'a Module,
+ T: &'a Type,
+ InAddressSpace: bool,
+ AddressSpace: c_uint,
) -> &'a Value;
- pub fn LLVMRustInsertPrivateGlobal<'a>(M: &'a Module, T: &'a Type) -> &'a Value;
pub fn LLVMGetFirstGlobal(M: &Module) -> Option<&Value>;
pub fn LLVMGetNextGlobal(GlobalVar: &Value) -> Option<&Value>;
pub fn LLVMDeleteGlobal(GlobalVar: &Value);
diff --git a/compiler/rustc_codegen_llvm/src/llvm/mod.rs b/compiler/rustc_codegen_llvm/src/llvm/mod.rs
index 4f5cc575da6..c32f13aa10b 100644
--- a/compiler/rustc_codegen_llvm/src/llvm/mod.rs
+++ b/compiler/rustc_codegen_llvm/src/llvm/mod.rs
@@ -200,9 +200,20 @@ pub fn set_section(llglobal: &Value, section_name: &str) {
}
}
-pub fn add_global<'a>(llmod: &'a Module, ty: &'a Type, name: &str) -> &'a Value {
+pub fn add_global<'a>(
+ llmod: &'a Module,
+ ty: &'a Type,
+ name: &str,
+ address_space: Option<u16>,
+) -> &'a Value {
let name_cstr = CString::new(name).expect("unexpected CString error");
- unsafe { LLVMAddGlobal(llmod, ty, name_cstr.as_ptr()) }
+ if let Some(address_space) = address_space {
+ unsafe {
+ LLVMAddGlobalInAddressSpace(llmod, ty, name_cstr.as_ptr(), address_space as c_uint)
+ }
+ } else {
+ unsafe { LLVMAddGlobal(llmod, ty, name_cstr.as_ptr()) }
+ }
}
pub fn set_initializer(llglobal: &Value, constant_val: &Value) {
diff --git a/compiler/rustc_codegen_llvm/src/mono_item.rs b/compiler/rustc_codegen_llvm/src/mono_item.rs
index 01e82339664..7dd3b5b661c 100644
--- a/compiler/rustc_codegen_llvm/src/mono_item.rs
+++ b/compiler/rustc_codegen_llvm/src/mono_item.rs
@@ -24,7 +24,7 @@ fn predefine_static(
let ty = instance.ty(self.tcx, ty::ParamEnv::reveal_all());
let llty = self.layout_of(ty).llvm_type(self);
- let g = self.define_global(symbol_name, llty).unwrap_or_else(|| {
+ let g = self.define_global(symbol_name, llty, None).unwrap_or_else(|| {
self.sess()
.emit_fatal(SymbolAlreadyDefined { span: self.tcx.def_span(def_id), symbol_name })
});
diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
index 4390486b0de..6fd4fdac33c 100644
--- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
+++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
@@ -176,8 +176,27 @@ extern "C" LLVMValueRef LLVMRustGetOrInsertFunction(LLVMModuleRef M,
);
}
+
+#if LLVM_VERSION_LT(16, 0)
+static Optional<unsigned> wrap_opt_address_space(bool InAddressSpace, unsigned AddressSpace) {
+#else
+static std::optional<unsigned> wrap_opt_address_space(bool InAddressSpace, unsigned AddressSpace) {
+#endif
+ if (InAddressSpace) {
+ return AddressSpace;
+ } else {
+#if LLVM_VERSION_LT(16, 0)
+ return None;
+#else
+ return std::nullopt;
+#endif
+ }
+}
+
+
extern "C" LLVMValueRef
-LLVMRustGetOrInsertGlobal(LLVMModuleRef M, const char *Name, size_t NameLen, LLVMTypeRef Ty) {
+LLVMRustGetOrInsertGlobal(LLVMModuleRef M, const char *Name, size_t NameLen, LLVMTypeRef Ty,
+ bool InAddressSpace, unsigned AddressSpace) {
Module *Mod = unwrap(M);
StringRef NameRef(Name, NameLen);
@@ -187,18 +206,30 @@ LLVMRustGetOrInsertGlobal(LLVMModuleRef M, const char *Name, size_t NameLen, LLV
// GlobalVariable* so we can access linkage, visibility, etc.
GlobalVariable *GV = Mod->getGlobalVariable(NameRef, true);
if (!GV)
- GV = new GlobalVariable(*Mod, unwrap(Ty), false,
- GlobalValue::ExternalLinkage, nullptr, NameRef);
+ GV = new GlobalVariable(*Mod,
+ unwrap(Ty),
+ false,
+ GlobalValue::ExternalLinkage,
+ nullptr,
+ NameRef,
+ nullptr,
+ GlobalValue::NotThreadLocal,
+ wrap_opt_address_space(InAddressSpace, AddressSpace));
return wrap(GV);
}
extern "C" LLVMValueRef
-LLVMRustInsertPrivateGlobal(LLVMModuleRef M, LLVMTypeRef Ty) {
+LLVMRustInsertPrivateGlobal(LLVMModuleRef M, LLVMTypeRef Ty,
+ bool InAddressSpace, unsigned AddressSpace) {
return wrap(new GlobalVariable(*unwrap(M),
unwrap(Ty),
false,
GlobalValue::PrivateLinkage,
- nullptr));
+ nullptr,
+ "",
+ nullptr,
+ GlobalValue::NotThreadLocal,
+ wrap_opt_address_space(InAddressSpace, AddressSpace)));
}
static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) {
--
2.42.0
From ade39b27998f2e9ab4dd658c503604c1a44c3bde Mon Sep 17 00:00:00 2001
From: Julia Tatz <[email protected]>
Date: Sun, 3 Jul 2022 22:32:56 -0400
Subject: [PATCH 5/5] nvptx fixes
---
compiler/rustc_target/src/abi/call/nvptx64.rs | 59 ++-----------------
.../src/spec/nvptx64_nvidia_cuda.rs | 6 +-
tests/assembly/asm/nvptx-types.rs | 12 ++--
3 files changed, 16 insertions(+), 61 deletions(-)
diff --git a/compiler/rustc_target/src/abi/call/nvptx64.rs b/compiler/rustc_target/src/abi/call/nvptx64.rs
index 4abe51cd697..3982a41752f 100644
--- a/compiler/rustc_target/src/abi/call/nvptx64.rs
+++ b/compiler/rustc_target/src/abi/call/nvptx64.rs
@@ -1,52 +1,12 @@
-use crate::abi::call::{ArgAbi, FnAbi, PassMode, Reg, Size, Uniform};
-use crate::abi::{HasDataLayout, TyAbiInterface};
-
-fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
- if ret.layout.is_aggregate() && ret.layout.size.bits() > 64 {
- ret.make_indirect();
- }
-}
+// Reference: PTX Writer's Guide to Interoperability
+// https://docs.nvidia.com/cuda/ptx-writers-guide-to-interoperability
-fn classify_arg<Ty>(arg: &mut ArgAbi<'_, Ty>) {
- if arg.layout.is_aggregate() && arg.layout.size.bits() > 64 {
- arg.make_indirect();
- }
-}
-
-fn classify_arg_kernel<'a, Ty, C>(_cx: &C, arg: &mut ArgAbi<'a, Ty>)
-where
- Ty: TyAbiInterface<'a, C> + Copy,
- C: HasDataLayout,
-{
- if matches!(arg.mode, PassMode::Pair(..)) && (arg.layout.is_adt() || arg.layout.is_tuple()) {
- let align_bytes = arg.layout.align.abi.bytes();
-
- let unit = match align_bytes {
- 1 => Reg::i8(),
- 2 => Reg::i16(),
- 4 => Reg::i32(),
- 8 => Reg::i64(),
- 16 => Reg::i128(),
- _ => unreachable!("Align is given as power of 2 no larger than 16 bytes"),
- };
- arg.cast_to(Uniform { unit, total: Size::from_bytes(2 * align_bytes) });
- }
-}
-
-pub fn compute_abi_info<Ty>(fn_abi: &mut FnAbi<'_, Ty>) {
- if !fn_abi.ret.is_ignore() {
- classify_ret(&mut fn_abi.ret);
- }
+use crate::abi::call::FnAbi;
+use crate::abi::{HasDataLayout, TyAbiInterface};
- for arg in fn_abi.args.iter_mut() {
- if arg.is_ignore() {
- continue;
- }
- classify_arg(arg);
- }
-}
+pub fn compute_abi_info<Ty>(_fn_abi: &mut FnAbi<'_, Ty>) {}
-pub fn compute_ptx_kernel_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
+pub fn compute_ptx_kernel_abi_info<'a, Ty, C>(_cx: &C, fn_abi: &mut FnAbi<'a, Ty>)
where
Ty: TyAbiInterface<'a, C> + Copy,
C: HasDataLayout,
@@ -54,11 +14,4 @@ pub fn compute_ptx_kernel_abi_info<'a, Ty, C>(cx: &C, fn_abi: &mut FnAbi<'a, Ty>
if !fn_abi.ret.layout.is_unit() && !fn_abi.ret.layout.is_never() {
panic!("Kernels should not return anything other than () or !");
}
-
- for arg in fn_abi.args.iter_mut() {
- if arg.is_ignore() {
- continue;
- }
- classify_arg_kernel(cx, arg);
- }
}
diff --git a/compiler/rustc_target/src/spec/nvptx64_nvidia_cuda.rs b/compiler/rustc_target/src/spec/nvptx64_nvidia_cuda.rs
index b0582b235b9..007753fb5a7 100644
--- a/compiler/rustc_target/src/spec/nvptx64_nvidia_cuda.rs
+++ b/compiler/rustc_target/src/spec/nvptx64_nvidia_cuda.rs
@@ -11,8 +11,8 @@ pub fn target() -> Target {
os: "cuda".into(),
vendor: "nvidia".into(),
linker_flavor: LinkerFlavor::Ptx,
- // The linker can be installed from `crates.io`.
- linker: Some("rust-ptx-linker".into()),
+ linker: None,
+ requires_lto: true,
// With `ptx-linker` approach, it can be later overridden via link flags.
cpu: "sm_30".into(),
@@ -28,7 +28,7 @@ pub fn target() -> Target {
// Avoid using dylib because it contain metadata not supported
// by LLVM NVPTX backend.
- only_cdylib: true,
+ // only_cdylib: true,
// Let the `ptx-linker` to handle LLVM lowering into MC / assembly.
obj_is_bitcode: true,
diff --git a/tests/assembly/asm/nvptx-types.rs b/tests/assembly/asm/nvptx-types.rs
index c319946b5f5..6282ee7ba88 100644
--- a/tests/assembly/asm/nvptx-types.rs
+++ b/tests/assembly/asm/nvptx-types.rs
@@ -1,9 +1,10 @@
// assembly-output: emit-asm
+// no-prefer-dynamic
// compile-flags: --target nvptx64-nvidia-cuda
-// compile-flags: --crate-type cdylib
// needs-llvm-components: nvptx
-#![feature(no_core, lang_items, rustc_attrs, asm_experimental_arch)]
+#![feature(no_core, lang_items, rustc_attrs, asm_sym, asm_experimental_arch)]
+#![crate_type = "cdylib"]
#![no_core]
#[rustc_builtin_macro]
@@ -30,9 +31,10 @@ impl Copy for i64 {}
impl Copy for f64 {}
impl Copy for ptr {}
-// NVPTX does not support static variables
-#[no_mangle]
-fn extern_func() {}
+extern "C" {
+ fn extern_func();
+ static extern_static: u32;
+}
// CHECK-LABEL: .visible .func sym_fn()
// CHECK: // begin inline asm
--
2.42.0