@@ -99,6 +99,17 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
9999
100100 let llret_ty = bx. backend_type ( bx. layout_of ( ret_ty) ) ;
101101
102+ let ret_llval = |bx : & mut Bx , llval| {
103+ if result. layout . ty . is_bool ( ) {
104+ OperandRef :: from_immediate_or_packed_pair ( bx, llval, result. layout )
105+ . val
106+ . store ( bx, result) ;
107+ } else if !result. layout . ty . is_unit ( ) {
108+ bx. store_to_place ( llval, result. val ) ;
109+ }
110+ Ok ( ( ) )
111+ } ;
112+
102113 let llval = match name {
103114 sym:: abort => {
104115 bx. abort ( ) ;
@@ -334,9 +345,48 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
334345 // This requires that atomic intrinsics follow a specific naming pattern:
335346 // "atomic_<operation>[_<ordering>]"
336347 name if let Some ( atomic) = name_str. strip_prefix ( "atomic_" ) => {
337- use crate :: common:: AtomicOrdering :: * ;
348+ use rustc_middle:: ty:: AtomicOrdering :: * ;
349+
338350 use crate :: common:: { AtomicRmwBinOp , SynchronizationScope } ;
339351
352+ let invalid_monomorphization = |ty| {
353+ bx. tcx ( ) . dcx ( ) . emit_err ( InvalidMonomorphization :: BasicIntegerType {
354+ span,
355+ name,
356+ ty,
357+ } ) ;
358+ } ;
359+
360+ let parse_const_generic_ordering = |ord : ty:: Value < ' tcx > | {
361+ let discr = ord. valtree . unwrap_branch ( ) [ 0 ] . unwrap_leaf ( ) ;
362+ discr. to_atomic_ordering ( )
363+ } ;
364+
365+ // Some intrinsics have the ordering already converted to a const generic parameter, we handle those first.
366+ match name {
367+ sym:: atomic_load => {
368+ let ty = fn_args. type_at ( 0 ) ;
369+ let ordering = fn_args. const_at ( 1 ) . to_value ( ) ;
370+ if !( int_type_width_signed ( ty, bx. tcx ( ) ) . is_some ( ) || ty. is_raw_ptr ( ) ) {
371+ invalid_monomorphization ( ty) ;
372+ return Ok ( ( ) ) ;
373+ }
374+ let layout = bx. layout_of ( ty) ;
375+ let source = args[ 0 ] . immediate ( ) ;
376+ let llval = bx. atomic_load (
377+ bx. backend_type ( layout) ,
378+ source,
379+ parse_const_generic_ordering ( ordering) ,
380+ layout. size ,
381+ ) ;
382+
383+ return ret_llval ( bx, llval) ;
384+ }
385+
386+ // The rest falls back to below.
387+ _ => { }
388+ }
389+
340390 let Some ( ( instruction, ordering) ) = atomic. split_once ( '_' ) else {
341391 bx. sess ( ) . dcx ( ) . emit_fatal ( errors:: MissingMemoryOrdering ) ;
342392 } ;
@@ -345,19 +395,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
345395 "relaxed" => Relaxed ,
346396 "acquire" => Acquire ,
347397 "release" => Release ,
348- "acqrel" => AcquireRelease ,
349- "seqcst" => SequentiallyConsistent ,
398+ "acqrel" => AcqRel ,
399+ "seqcst" => SeqCst ,
350400 _ => bx. sess ( ) . dcx ( ) . emit_fatal ( errors:: UnknownAtomicOrdering ) ,
351401 } ;
352402
353- let invalid_monomorphization = |ty| {
354- bx. tcx ( ) . dcx ( ) . emit_err ( InvalidMonomorphization :: BasicIntegerType {
355- span,
356- name,
357- ty,
358- } ) ;
359- } ;
360-
361403 match instruction {
362404 "cxchg" | "cxchgweak" => {
363405 let Some ( ( success, failure) ) = ordering. split_once ( '_' ) else {
@@ -390,24 +432,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
390432 return Ok ( ( ) ) ;
391433 }
392434
393- "load" => {
394- let ty = fn_args. type_at ( 0 ) ;
395- if int_type_width_signed ( ty, bx. tcx ( ) ) . is_some ( ) || ty. is_raw_ptr ( ) {
396- let layout = bx. layout_of ( ty) ;
397- let size = layout. size ;
398- let source = args[ 0 ] . immediate ( ) ;
399- bx. atomic_load (
400- bx. backend_type ( layout) ,
401- source,
402- parse_ordering ( bx, ordering) ,
403- size,
404- )
405- } else {
406- invalid_monomorphization ( ty) ;
407- return Ok ( ( ) ) ;
408- }
409- }
410-
411435 "store" => {
412436 let ty = fn_args. type_at ( 0 ) ;
413437 if int_type_width_signed ( ty, bx. tcx ( ) ) . is_some ( ) || ty. is_raw_ptr ( ) {
@@ -538,14 +562,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
538562 }
539563 } ;
540564
541- if result. layout . ty . is_bool ( ) {
542- OperandRef :: from_immediate_or_packed_pair ( bx, llval, result. layout )
543- . val
544- . store ( bx, result) ;
545- } else if !result. layout . ty . is_unit ( ) {
546- bx. store_to_place ( llval, result. val ) ;
547- }
548- Ok ( ( ) )
565+ ret_llval ( bx, llval)
549566 }
550567}
551568
0 commit comments