@@ -29,21 +29,29 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
2929 }
3030
3131 fn run_pass ( & self , tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) {
32+ let def_id = body. source . def_id ( ) ;
3233 let ctx = InstSimplifyContext {
3334 tcx,
3435 local_decls : & body. local_decls ,
3536 typing_env : body. typing_env ( tcx) ,
3637 } ;
3738 let preserve_ub_checks =
3839 attr:: contains_name ( tcx. hir_krate_attrs ( ) , sym:: rustc_preserve_ub_checks) ;
40+ let remove_ub_checks = if tcx. is_coroutine ( def_id) {
41+ false
42+ } else {
43+ tcx. has_attr ( def_id, sym:: rustc_no_ubchecks)
44+ } ;
3945 for block in body. basic_blocks . as_mut ( ) {
4046 for statement in block. statements . iter_mut ( ) {
4147 let StatementKind :: Assign ( box ( .., rvalue) ) = & mut statement. kind else {
4248 continue ;
4349 } ;
4450
45- if !preserve_ub_checks {
46- ctx. simplify_ub_check ( rvalue) ;
51+ if remove_ub_checks {
52+ ctx. simplify_ub_check ( rvalue, false ) ;
53+ } else if !preserve_ub_checks {
54+ ctx. simplify_ub_check ( rvalue, tcx. sess . ub_checks ( ) ) ;
4755 }
4856 ctx. simplify_bool_cmp ( rvalue) ;
4957 ctx. simplify_ref_deref ( rvalue) ;
@@ -168,13 +176,13 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
168176 }
169177 }
170178
171- fn simplify_ub_check ( & self , rvalue : & mut Rvalue < ' tcx > ) {
179+ fn simplify_ub_check ( & self , rvalue : & mut Rvalue < ' tcx > , ub_checks : bool ) {
172180 // FIXME: Should we do the same for overflow checks?
173181 let Rvalue :: NullaryOp ( NullOp :: RuntimeChecks ( RuntimeChecks :: UbChecks ) ) = * rvalue else {
174182 return ;
175183 } ;
176184
177- let const_ = Const :: from_bool ( self . tcx , self . tcx . sess . ub_checks ( ) ) ;
185+ let const_ = Const :: from_bool ( self . tcx , ub_checks) ;
178186 let constant = ConstOperand { span : DUMMY_SP , const_, user_ty : None } ;
179187 * rvalue = Rvalue :: Use ( Operand :: Constant ( Box :: new ( constant) ) ) ;
180188 }
0 commit comments