@@ -415,9 +415,36 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
415415 i += 1 ;
416416 i - 1
417417 } ;
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+ {
423+ if let abi:: Abi :: Scalar ( scalar) = abi {
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+ if let Int ( ..) = scalar. primitive ( ) {
428+ if !scalar. is_bool ( ) && !scalar. is_always_valid ( cx) {
429+ attributes:: apply_to_llfn (
430+ llfn,
431+ idx,
432+ & [ llvm:: CreateRangeAttr (
433+ cx. llcx ,
434+ scalar. size ( cx) ,
435+ scalar. valid_range ( cx) ,
436+ ) ] ,
437+ ) ;
438+ }
439+ }
440+ }
441+ }
442+ } ;
443+
418444 match & self . ret . mode {
419445 PassMode :: Direct ( attrs) => {
420446 attrs. apply_attrs_to_llfn ( llvm:: AttributePlace :: ReturnValue , cx, llfn) ;
447+ apply_range_attr ( llvm:: AttributePlace :: ReturnValue , self . ret . layout . abi ) ;
421448 }
422449 PassMode :: Indirect { attrs, meta_attrs : _, on_stack } => {
423450 assert ! ( !on_stack) ;
@@ -456,8 +483,11 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
456483 ) ;
457484 attributes:: apply_to_llfn ( llfn, llvm:: AttributePlace :: Argument ( i) , & [ byval] ) ;
458485 }
459- PassMode :: Direct ( attrs)
460- | PassMode :: Indirect { attrs, meta_attrs : None , on_stack : false } => {
486+ PassMode :: Direct ( attrs) => {
487+ let i = apply ( attrs) ;
488+ apply_range_attr ( llvm:: AttributePlace :: Argument ( i) , arg. layout . abi ) ;
489+ }
490+ PassMode :: Indirect { attrs, meta_attrs : None , on_stack : false } => {
461491 apply ( attrs) ;
462492 }
463493 PassMode :: Indirect { attrs, meta_attrs : Some ( meta_attrs) , on_stack } => {
@@ -517,13 +547,17 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
517547 }
518548 _ => { }
519549 }
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) ) ;
550+ if bx. cx . sess ( ) . opts . optimize != config:: OptLevel :: No
551+ && llvm_util:: get_version ( ) < ( 19 , 0 , 0 )
552+ {
553+ if let abi:: Abi :: Scalar ( scalar) = self . ret . layout . abi {
554+ // If the value is a boolean, the range is 0..2 and that ultimately
555+ // become 0..0 when the type becomes i1, which would be rejected
556+ // by the LLVM verifier.
557+ if let Int ( ..) = scalar. primitive ( ) {
558+ if !scalar. is_bool ( ) && !scalar. is_always_valid ( bx) {
559+ bx. range_metadata ( callsite, scalar. valid_range ( bx) ) ;
560+ }
527561 }
528562 }
529563 }
0 commit comments