Skip to content

Commit 8549c0a

Browse files
committed
Add intrinsic body fallback to cranelift and use it
1 parent 531505f commit 8549c0a

File tree

2 files changed

+32
-35
lines changed
  • compiler/rustc_codegen_cranelift/src

2 files changed

+32
-35
lines changed

compiler/rustc_codegen_cranelift/src/abi/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -387,15 +387,19 @@ pub(crate) fn codegen_terminator_call<'tcx>(
387387

388388
match instance.def {
389389
InstanceDef::Intrinsic(_) => {
390-
crate::intrinsics::codegen_intrinsic_call(
390+
match crate::intrinsics::codegen_intrinsic_call(
391391
fx,
392392
instance,
393393
args,
394394
ret_place,
395395
target,
396396
source_info,
397-
);
398-
return;
397+
) {
398+
Ok(()) => return,
399+
// Unimplemented intrinsics must have a fallback body. The fallback body is obtained
400+
// by converting the `InstanceDef::Intrinsic` to an `InstanceDef::Item`.
401+
Err(()) => Some(Instance::new(instance.def_id(), instance.args)),
402+
}
399403
}
400404
InstanceDef::DropGlue(_, None) => {
401405
// empty drop glue - a nop.

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

+25-32
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
268268
destination: CPlace<'tcx>,
269269
target: Option<BasicBlock>,
270270
source_info: mir::SourceInfo,
271-
) {
271+
) -> Result<(), ()> {
272272
let intrinsic = fx.tcx.item_name(instance.def_id());
273273
let instance_args = instance.args;
274274

@@ -295,8 +295,9 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
295295
destination,
296296
target,
297297
source_info,
298-
);
298+
)?;
299299
}
300+
Ok(())
300301
}
301302

302303
fn codegen_float_intrinsic_call<'tcx>(
@@ -430,25 +431,20 @@ fn codegen_regular_intrinsic_call<'tcx>(
430431
ret: CPlace<'tcx>,
431432
destination: Option<BasicBlock>,
432433
source_info: mir::SourceInfo,
433-
) {
434+
) -> Result<(), ()> {
435+
assert_eq!(generic_args, instance.args);
434436
let usize_layout = fx.layout_of(fx.tcx.types.usize);
435437

436438
match intrinsic {
437439
sym::abort => {
438440
fx.bcx.ins().trap(TrapCode::User(0));
439-
return;
441+
return Ok(());
440442
}
441443
sym::likely | sym::unlikely => {
442444
intrinsic_args!(fx, args => (a); intrinsic);
443445

444446
ret.write_cvalue(fx, a);
445447
}
446-
sym::is_val_statically_known => {
447-
intrinsic_args!(fx, args => (_a); intrinsic);
448-
449-
let res = fx.bcx.ins().iconst(types::I8, 0);
450-
ret.write_cvalue(fx, CValue::by_val(res, ret.layout()));
451-
}
452448
sym::breakpoint => {
453449
intrinsic_args!(fx, args => (); intrinsic);
454450

@@ -697,7 +693,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
697693
})
698694
});
699695
crate::base::codegen_panic_nounwind(fx, &msg_str, Some(source_info.span));
700-
return;
696+
return Ok(());
701697
}
702698
}
703699
}
@@ -792,7 +788,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
792788
if fx.tcx.is_compiler_builtins(LOCAL_CRATE) {
793789
// special case for compiler-builtins to avoid having to patch it
794790
crate::trap::trap_unimplemented(fx, "128bit atomics not yet supported");
795-
return;
791+
return Ok(());
796792
} else {
797793
fx.tcx
798794
.dcx()
@@ -802,7 +798,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
802798
ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
803799
_ => {
804800
report_atomic_type_validation_error(fx, intrinsic, source_info.span, ty);
805-
return;
801+
return Ok(());
806802
}
807803
}
808804
let clif_ty = fx.clif_type(ty).unwrap();
@@ -823,7 +819,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
823819
if fx.tcx.is_compiler_builtins(LOCAL_CRATE) {
824820
// special case for compiler-builtins to avoid having to patch it
825821
crate::trap::trap_unimplemented(fx, "128bit atomics not yet supported");
826-
return;
822+
return Ok(());
827823
} else {
828824
fx.tcx
829825
.dcx()
@@ -833,7 +829,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
833829
ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
834830
_ => {
835831
report_atomic_type_validation_error(fx, intrinsic, source_info.span, ty);
836-
return;
832+
return Ok(());
837833
}
838834
}
839835

@@ -850,7 +846,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
850846
ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
851847
_ => {
852848
report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
853-
return;
849+
return Ok(());
854850
}
855851
}
856852
let ty = fx.clif_type(layout.ty).unwrap();
@@ -872,7 +868,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
872868
ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
873869
_ => {
874870
report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
875-
return;
871+
return Ok(());
876872
}
877873
}
878874

@@ -895,7 +891,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
895891
ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
896892
_ => {
897893
report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
898-
return;
894+
return Ok(());
899895
}
900896
}
901897
let ty = fx.clif_type(layout.ty).unwrap();
@@ -917,7 +913,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
917913
ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
918914
_ => {
919915
report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
920-
return;
916+
return Ok(());
921917
}
922918
}
923919
let ty = fx.clif_type(layout.ty).unwrap();
@@ -939,7 +935,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
939935
ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
940936
_ => {
941937
report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
942-
return;
938+
return Ok(());
943939
}
944940
}
945941
let ty = fx.clif_type(layout.ty).unwrap();
@@ -960,7 +956,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
960956
ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
961957
_ => {
962958
report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
963-
return;
959+
return Ok(());
964960
}
965961
}
966962
let ty = fx.clif_type(layout.ty).unwrap();
@@ -981,7 +977,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
981977
ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
982978
_ => {
983979
report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
984-
return;
980+
return Ok(());
985981
}
986982
}
987983
let ty = fx.clif_type(layout.ty).unwrap();
@@ -1002,7 +998,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
1002998
ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
1003999
_ => {
10041000
report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1005-
return;
1001+
return Ok(());
10061002
}
10071003
}
10081004
let ty = fx.clif_type(layout.ty).unwrap();
@@ -1023,7 +1019,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
10231019
ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
10241020
_ => {
10251021
report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1026-
return;
1022+
return Ok(());
10271023
}
10281024
}
10291025
let ty = fx.clif_type(layout.ty).unwrap();
@@ -1044,7 +1040,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
10441040
ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
10451041
_ => {
10461042
report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1047-
return;
1043+
return Ok(());
10481044
}
10491045
}
10501046
let ty = fx.clif_type(layout.ty).unwrap();
@@ -1065,7 +1061,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
10651061
ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
10661062
_ => {
10671063
report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1068-
return;
1064+
return Ok(());
10691065
}
10701066
}
10711067
let ty = fx.clif_type(layout.ty).unwrap();
@@ -1086,7 +1082,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
10861082
ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {}
10871083
_ => {
10881084
report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty);
1089-
return;
1085+
return Ok(());
10901086
}
10911087
}
10921088
let ty = fx.clif_type(layout.ty).unwrap();
@@ -1261,13 +1257,10 @@ fn codegen_regular_intrinsic_call<'tcx>(
12611257
);
12621258
}
12631259

1264-
_ => {
1265-
fx.tcx
1266-
.dcx()
1267-
.span_fatal(source_info.span, format!("unsupported intrinsic {}", intrinsic));
1268-
}
1260+
_ => return Err(()),
12691261
}
12701262

12711263
let ret_block = fx.get_block(destination.unwrap());
12721264
fx.bcx.ins().jump(ret_block, &[]);
1265+
Ok(())
12731266
}

0 commit comments

Comments
 (0)