@@ -30,21 +30,29 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
3030 }
3131
3232 fn run_pass ( & self , tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) {
33+ let def_id = body. source . def_id ( ) ;
3334 let ctx = InstSimplifyContext {
3435 tcx,
3536 local_decls : & body. local_decls ,
3637 typing_env : body. typing_env ( tcx) ,
3738 } ;
3839 let preserve_ub_checks =
3940 attr:: contains_name ( tcx. hir_krate_attrs ( ) , sym:: rustc_preserve_ub_checks) ;
41+ let remove_ub_checks = if tcx. is_coroutine ( def_id) {
42+ false
43+ } else {
44+ tcx. has_attr ( def_id, sym:: rustc_no_ubchecks)
45+ } ;
4046 for block in body. basic_blocks . as_mut ( ) {
4147 for statement in block. statements . iter_mut ( ) {
4248 let StatementKind :: Assign ( box ( .., rvalue) ) = & mut statement. kind else {
4349 continue ;
4450 } ;
4551
46- if !preserve_ub_checks {
47- ctx. simplify_ub_check ( rvalue) ;
52+ if remove_ub_checks {
53+ ctx. simplify_ub_check ( rvalue, false ) ;
54+ } else if !preserve_ub_checks {
55+ ctx. simplify_ub_check ( rvalue, tcx. sess . ub_checks ( ) ) ;
4856 }
4957 ctx. simplify_bool_cmp ( rvalue) ;
5058 ctx. simplify_ref_deref ( rvalue) ;
@@ -168,10 +176,10 @@ 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 let Rvalue :: NullaryOp ( NullOp :: UbChecks , _) = * rvalue else { return } ;
173181
174- let const_ = Const :: from_bool ( self . tcx , self . tcx . sess . ub_checks ( ) ) ;
182+ let const_ = Const :: from_bool ( self . tcx , ub_checks) ;
175183 let constant = ConstOperand { span : DUMMY_SP , const_, user_ty : None } ;
176184 * rvalue = Rvalue :: Use ( Operand :: Constant ( Box :: new ( constant) ) ) ;
177185 }
0 commit comments