@@ -31,19 +31,27 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
3131 }
3232
3333 fn run_pass ( & self , tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) {
34+ let def_id = body. source . def_id ( ) ;
3435 let ctx = InstSimplifyContext {
3536 tcx,
3637 local_decls : & body. local_decls ,
3738 typing_env : body. typing_env ( tcx) ,
3839 } ;
3940 let preserve_ub_checks =
4041 attr:: contains_name ( tcx. hir ( ) . krate_attrs ( ) , sym:: rustc_preserve_ub_checks) ;
42+ let remove_ub_checks = if tcx. is_coroutine ( def_id) {
43+ false
44+ } else {
45+ tcx. has_attr ( def_id, sym:: rustc_no_ubchecks)
46+ } ;
4147 for block in body. basic_blocks . as_mut ( ) {
4248 for statement in block. statements . iter_mut ( ) {
4349 match statement. kind {
4450 StatementKind :: Assign ( box ( _place, ref mut rvalue) ) => {
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) ;
@@ -160,9 +168,9 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
160168 }
161169 }
162170
163- fn simplify_ub_check ( & self , rvalue : & mut Rvalue < ' tcx > ) {
171+ fn simplify_ub_check ( & self , rvalue : & mut Rvalue < ' tcx > , ub_checks : bool ) {
164172 if let Rvalue :: NullaryOp ( NullOp :: UbChecks , _) = * rvalue {
165- let const_ = Const :: from_bool ( self . tcx , self . tcx . sess . ub_checks ( ) ) ;
173+ let const_ = Const :: from_bool ( self . tcx , ub_checks) ;
166174 let constant = ConstOperand { span : DUMMY_SP , const_, user_ty : None } ;
167175 * rvalue = Rvalue :: Use ( Operand :: Constant ( Box :: new ( constant) ) ) ;
168176 }
0 commit comments