@@ -67,7 +67,6 @@ impl<'a, 'll> SBuilder<'a, 'll> {
67
67
) -> & ' ll Value {
68
68
debug ! ( "call {:?} with args ({:?})" , llfn, args) ;
69
69
70
- let args = self . check_call ( "call" , llty, llfn, args) ;
71
70
let funclet_bundle = funclet. map ( |funclet| funclet. bundle ( ) ) ;
72
71
let mut bundles: SmallVec < [ _ ; 2 ] > = SmallVec :: new ( ) ;
73
72
if let Some ( funclet_bundle) = funclet_bundle {
@@ -349,7 +348,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
349
348
) -> & ' ll Value {
350
349
debug ! ( "invoke {:?} with args ({:?})" , llfn, args) ;
351
350
352
- let args = self . check_call ( "invoke" , llty, llfn, args) ;
351
+ let args = self . cast_arguments ( "invoke" , llty, llfn, args, fn_abi . is_some ( ) ) ;
353
352
let funclet_bundle = funclet. map ( |funclet| funclet. bundle ( ) ) ;
354
353
let mut bundles: SmallVec < [ _ ; 2 ] > = SmallVec :: new ( ) ;
355
354
if let Some ( funclet_bundle) = funclet_bundle {
@@ -381,8 +380,10 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
381
380
} ;
382
381
if let Some ( fn_abi) = fn_abi {
383
382
fn_abi. apply_attrs_callsite ( self , invoke, llfn) ;
383
+ self . cast_return ( fn_abi, llfn, invoke)
384
+ } else {
385
+ invoke
384
386
}
385
- invoke
386
387
}
387
388
388
389
fn unreachable ( & mut self ) {
@@ -1404,7 +1405,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
1404
1405
) -> & ' ll Value {
1405
1406
debug ! ( "call {:?} with args ({:?})" , llfn, args) ;
1406
1407
1407
- let args = self . check_call ( "call" , llty, llfn, args) ;
1408
+ let args = self . cast_arguments ( "call" , llty, llfn, args, fn_abi . is_some ( ) ) ;
1408
1409
let funclet_bundle = funclet. map ( |funclet| funclet. bundle ( ) ) ;
1409
1410
let mut bundles: SmallVec < [ _ ; 2 ] > = SmallVec :: new ( ) ;
1410
1411
if let Some ( funclet_bundle) = funclet_bundle {
@@ -1434,8 +1435,10 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
1434
1435
} ;
1435
1436
if let Some ( fn_abi) = fn_abi {
1436
1437
fn_abi. apply_attrs_callsite ( self , call, llfn) ;
1438
+ self . cast_return ( fn_abi, llfn, call)
1439
+ } else {
1440
+ call
1437
1441
}
1438
- call
1439
1442
}
1440
1443
1441
1444
fn zext ( & mut self , val : & ' ll Value , dest_ty : & ' ll Type ) -> & ' ll Value {
@@ -1602,47 +1605,6 @@ impl<'a, 'll, CX: Borrow<SCx<'ll>>> GenericBuilder<'a, 'll, CX> {
1602
1605
ret. expect ( "LLVM does not have support for catchret" )
1603
1606
}
1604
1607
1605
- fn check_call < ' b > (
1606
- & mut self ,
1607
- typ : & str ,
1608
- fn_ty : & ' ll Type ,
1609
- llfn : & ' ll Value ,
1610
- args : & ' b [ & ' ll Value ] ,
1611
- ) -> Cow < ' b , [ & ' ll Value ] > {
1612
- assert ! (
1613
- self . cx. type_kind( fn_ty) == TypeKind :: Function ,
1614
- "builder::{typ} not passed a function, but {fn_ty:?}"
1615
- ) ;
1616
-
1617
- let param_tys = self . cx . func_params_types ( fn_ty) ;
1618
-
1619
- let all_args_match = iter:: zip ( & param_tys, args. iter ( ) . map ( |& v| self . cx . val_ty ( v) ) )
1620
- . all ( |( expected_ty, actual_ty) | * expected_ty == actual_ty) ;
1621
-
1622
- if all_args_match {
1623
- return Cow :: Borrowed ( args) ;
1624
- }
1625
-
1626
- let casted_args: Vec < _ > = iter:: zip ( param_tys, args)
1627
- . enumerate ( )
1628
- . map ( |( i, ( expected_ty, & actual_val) ) | {
1629
- let actual_ty = self . cx . val_ty ( actual_val) ;
1630
- if expected_ty != actual_ty {
1631
- debug ! (
1632
- "type mismatch in function call of {:?}. \
1633
- Expected {:?} for param {}, got {:?}; injecting bitcast",
1634
- llfn, expected_ty, i, actual_ty
1635
- ) ;
1636
- self . bitcast ( actual_val, expected_ty)
1637
- } else {
1638
- actual_val
1639
- }
1640
- } )
1641
- . collect ( ) ;
1642
-
1643
- Cow :: Owned ( casted_args)
1644
- }
1645
-
1646
1608
pub ( crate ) fn va_arg ( & mut self , list : & ' ll Value , ty : & ' ll Type ) -> & ' ll Value {
1647
1609
unsafe { llvm:: LLVMBuildVAArg ( self . llbuilder , list, ty, UNNAMED ) }
1648
1610
}
@@ -1714,6 +1676,93 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
1714
1676
self . call ( self . type_func ( & [ src_ty] , dest_ty) , None , None , f, & [ val] , None , None )
1715
1677
}
1716
1678
1679
+ fn autocast (
1680
+ & mut self ,
1681
+ llfn : & ' ll Value ,
1682
+ val : & ' ll Value ,
1683
+ src_ty : & ' ll Type ,
1684
+ dest_ty : & ' ll Type ,
1685
+ is_argument : bool ,
1686
+ ) -> & ' ll Value {
1687
+ let ( rust_ty, llvm_ty) = if is_argument { ( src_ty, dest_ty) } else { ( dest_ty, src_ty) } ;
1688
+
1689
+ if rust_ty == llvm_ty {
1690
+ return val;
1691
+ }
1692
+
1693
+ match self . type_kind ( llvm_ty) {
1694
+ TypeKind :: Struct => {
1695
+ let mut ret = self . const_poison ( dest_ty) ;
1696
+ for ( idx, ( src_element_ty, dest_element_ty) ) in
1697
+ iter:: zip ( self . struct_element_types ( src_ty) , self . struct_element_types ( dest_ty) )
1698
+ . enumerate ( )
1699
+ {
1700
+ let elt = self . extract_value ( val, idx as u64 ) ;
1701
+ let casted_elt =
1702
+ self . autocast ( llfn, elt, src_element_ty, dest_element_ty, is_argument) ;
1703
+ ret = self . insert_value ( ret, casted_elt, idx as u64 ) ;
1704
+ }
1705
+ ret
1706
+ }
1707
+ _ => unreachable ! ( ) ,
1708
+ }
1709
+ }
1710
+
1711
+ fn cast_arguments < ' b > (
1712
+ & mut self ,
1713
+ typ : & str ,
1714
+ fn_ty : & ' ll Type ,
1715
+ llfn : & ' ll Value ,
1716
+ args : & ' b [ & ' ll Value ] ,
1717
+ has_fnabi : bool ,
1718
+ ) -> Cow < ' b , [ & ' ll Value ] > {
1719
+ assert_eq ! (
1720
+ self . type_kind( fn_ty) ,
1721
+ TypeKind :: Function ,
1722
+ "{typ} not passed a function, but {fn_ty:?}"
1723
+ ) ;
1724
+
1725
+ let param_tys = self . func_params_types ( fn_ty) ;
1726
+
1727
+ let mut casted_args = Cow :: Borrowed ( args) ;
1728
+
1729
+ for ( idx, ( dest_ty, & arg) ) in iter:: zip ( param_tys, args) . enumerate ( ) {
1730
+ let src_ty = self . val_ty ( arg) ;
1731
+ assert ! (
1732
+ self . equate_ty( src_ty, dest_ty) ,
1733
+ "Cannot match `{dest_ty:?}` (expected) with `{src_ty:?}` (found) in `{llfn:?}`"
1734
+ ) ;
1735
+
1736
+ let casted_arg = self . autocast ( llfn, arg, src_ty, dest_ty, true ) ;
1737
+ if arg != casted_arg {
1738
+ assert ! (
1739
+ has_fnabi,
1740
+ "Should inject autocasts in function call of {llfn:?}, but not able to get Rust signature"
1741
+ ) ;
1742
+
1743
+ casted_args. to_mut ( ) [ idx] = casted_arg;
1744
+ }
1745
+ }
1746
+
1747
+ casted_args
1748
+ }
1749
+
1750
+ fn cast_return (
1751
+ & mut self ,
1752
+ fn_abi : & FnAbi < ' tcx , Ty < ' tcx > > ,
1753
+ llfn : & ' ll Value ,
1754
+ ret : & ' ll Value ,
1755
+ ) -> & ' ll Value {
1756
+ let src_ty = self . val_ty ( ret) ;
1757
+ let dest_ty = fn_abi. llvm_return_type ( self ) ;
1758
+ assert ! (
1759
+ self . equate_ty( dest_ty, src_ty) ,
1760
+ "Cannot match `{src_ty:?}` (expected) with `{dest_ty:?}` (found) in `{llfn:?}`"
1761
+ ) ;
1762
+
1763
+ self . autocast ( llfn, ret, src_ty, dest_ty, false )
1764
+ }
1765
+
1717
1766
pub ( crate ) fn landing_pad (
1718
1767
& mut self ,
1719
1768
ty : & ' ll Type ,
@@ -1743,7 +1792,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
1743
1792
) -> & ' ll Value {
1744
1793
debug ! ( "invoke {:?} with args ({:?})" , llfn, args) ;
1745
1794
1746
- let args = self . check_call ( "callbr" , llty, llfn, args) ;
1795
+ let args = self . cast_arguments ( "callbr" , llty, llfn, args, fn_abi . is_some ( ) ) ;
1747
1796
let funclet_bundle = funclet. map ( |funclet| funclet. bundle ( ) ) ;
1748
1797
let mut bundles: SmallVec < [ _ ; 2 ] > = SmallVec :: new ( ) ;
1749
1798
if let Some ( funclet_bundle) = funclet_bundle {
@@ -1776,8 +1825,10 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
1776
1825
} ;
1777
1826
if let Some ( fn_abi) = fn_abi {
1778
1827
fn_abi. apply_attrs_callsite ( self , callbr, llfn) ;
1828
+ self . cast_return ( fn_abi, llfn, callbr)
1829
+ } else {
1830
+ callbr
1779
1831
}
1780
- callbr
1781
1832
}
1782
1833
1783
1834
// Emits CFI pointer type membership tests.
0 commit comments