@@ -415,9 +415,31 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
415
415
i += 1 ;
416
416
i - 1
417
417
} ;
418
+
419
+ let apply_range_attr = |idx : AttributePlace , abi : rustc_target:: abi:: Abi | {
420
+ if cx. sess ( ) . opts . optimize != config:: OptLevel :: No
421
+ && llvm_util:: get_version ( ) >= ( 19 , 0 , 0 )
422
+ && let abi:: Abi :: Scalar ( scalar) = abi
423
+ && matches ! ( scalar. primitive( ) , Int ( ..) )
424
+ // If the value is a boolean, the range is 0..2 and that ultimately
425
+ // become 0..0 when the type becomes i1, which would be rejected
426
+ // by the LLVM verifier.
427
+ && !scalar. is_bool ( )
428
+ // LLVM also rejects full range.
429
+ && !scalar. is_always_valid ( cx)
430
+ {
431
+ attributes:: apply_to_llfn (
432
+ llfn,
433
+ idx,
434
+ & [ llvm:: CreateRangeAttr ( cx. llcx , scalar. size ( cx) , scalar. valid_range ( cx) ) ] ,
435
+ ) ;
436
+ }
437
+ } ;
438
+
418
439
match & self . ret . mode {
419
440
PassMode :: Direct ( attrs) => {
420
441
attrs. apply_attrs_to_llfn ( llvm:: AttributePlace :: ReturnValue , cx, llfn) ;
442
+ apply_range_attr ( llvm:: AttributePlace :: ReturnValue , self . ret . layout . abi ) ;
421
443
}
422
444
PassMode :: Indirect { attrs, meta_attrs : _, on_stack } => {
423
445
assert ! ( !on_stack) ;
@@ -456,8 +478,11 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
456
478
) ;
457
479
attributes:: apply_to_llfn ( llfn, llvm:: AttributePlace :: Argument ( i) , & [ byval] ) ;
458
480
}
459
- PassMode :: Direct ( attrs)
460
- | PassMode :: Indirect { attrs, meta_attrs : None , on_stack : false } => {
481
+ PassMode :: Direct ( attrs) => {
482
+ let i = apply ( attrs) ;
483
+ apply_range_attr ( llvm:: AttributePlace :: Argument ( i) , arg. layout . abi ) ;
484
+ }
485
+ PassMode :: Indirect { attrs, meta_attrs : None , on_stack : false } => {
461
486
apply ( attrs) ;
462
487
}
463
488
PassMode :: Indirect { attrs, meta_attrs : Some ( meta_attrs) , on_stack } => {
@@ -517,15 +542,18 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
517
542
}
518
543
_ => { }
519
544
}
520
- if let abi:: Abi :: Scalar ( scalar) = self . ret . layout . abi {
521
- // If the value is a boolean, the range is 0..2 and that ultimately
522
- // become 0..0 when the type becomes i1, which would be rejected
523
- // by the LLVM verifier.
524
- if let Int ( ..) = scalar. primitive ( ) {
525
- if !scalar. is_bool ( ) && !scalar. is_always_valid ( bx) {
526
- bx. range_metadata ( callsite, scalar. valid_range ( bx) ) ;
527
- }
528
- }
545
+ if bx. cx . sess ( ) . opts . optimize != config:: OptLevel :: No
546
+ && llvm_util:: get_version ( ) < ( 19 , 0 , 0 )
547
+ && let abi:: Abi :: Scalar ( scalar) = self . ret . layout . abi
548
+ && matches ! ( scalar. primitive( ) , Int ( ..) )
549
+ // If the value is a boolean, the range is 0..2 and that ultimately
550
+ // become 0..0 when the type becomes i1, which would be rejected
551
+ // by the LLVM verifier.
552
+ && !scalar. is_bool ( )
553
+ // LLVM also rejects full range.
554
+ && !scalar. is_always_valid ( bx)
555
+ {
556
+ bx. range_metadata ( callsite, scalar. valid_range ( bx) ) ;
529
557
}
530
558
for arg in self . args . iter ( ) {
531
559
match & arg. mode {
0 commit comments