Skip to content

Commit

Permalink
[EXPERIMENTAL] [Wont compile] - trying different options on how to lo…
Browse files Browse the repository at this point in the history
…calize fn generic_simd_intrinsic
  • Loading branch information
JhonnyBillM committed Nov 17, 2022
1 parent 9a31b3c commit 64aee83
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
82 changes: 82 additions & 0 deletions compiler/rustc_codegen_llvm/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,8 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
llret_ty: &'ll Type,
span: Span,
) -> Result<&'ll Value, ()> {
// Existing macros:

// macros for error handling:
#[allow(unused_macro_rules)]
macro_rules! emit_error {
Expand Down Expand Up @@ -870,13 +872,50 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
};
}

// [Experimental] macros for localized error handling:

macro_rules! return_localized_error {
($diag: tt) => {{
bx.sess().emit_err($diag);
return Err(());
}};
}

macro_rules! require_localized {
($cond: expr, $diag: tt) => {
if !$cond {
return_localized_error!($diag);
}
};
}

macro_rules! require_simd_localized {
($ty: expr, $diag: tt) => {
require_localized!($ty.is_simd(), $diag)
};
}

let tcx = bx.tcx();
let sig =
tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), callee_ty.fn_sig(tcx));
let arg_tys = sig.inputs();

if name == sym::simd_select_bitmask {
// Example of the two options presented above:

// 1. Existing code: macro passing non-translatable strings.
require_simd!(arg_tys[1], "argument");

// 2. Macro accepting diagnostics.
let diag = InvalidMonomorphization::SimdArgument { span, name, ty: arg_tys[1] };
require_simd_localized!(arg_tys[1], diag);

// 3. No macros: evaluating condition, emitting diagnostic and returning error.
if !arg_tys[1].is_simd() {
tcx.sess.emit_err(InvalidMonomorphization::SimdArgument { span, name, ty: arg_tys[1] });
return Err(());
}

let (len, _) = arg_tys[1].simd_size_and_type(bx.tcx());

let expected_int_bits = (len.max(8) - 1).next_power_of_two();
Expand Down Expand Up @@ -1298,12 +1337,55 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
// * T: type of the element to load
// * M: any integer width is supported, will be truncated to i1

// Example of the two options presented above:

// 1. Existing code: macro passing non-translatable strings.
// All types must be simd vector types
require_simd!(in_ty, "first");
require_simd!(arg_tys[1], "second");
require_simd!(arg_tys[2], "third");
require_simd!(ret_ty, "return");

// 2. Macro accepting diagnostics.
let diag = InvalidMonomorphization::SimdFirstType { span, name, in_ty };
require_simd!(in_ty, diag);
let diag = InvalidMonomorphization::SimdSecondType { span, name, in_ty: arg_tys[1] };
require_simd!(arg_tys[1], diag);
let diag = InvalidMonomorphization::SimdThirdType { span, name, in_ty: arg_tys[2] };
require_simd!(arg_tys[2], diag);
let diag = InvalidMonomorphization::SimdReturnType { span, name, in_ty: ret_ty };
require_simd!(ret_ty, diag);

// 3. No macros: evaluating condition, emitting diagnostic and returning error.
if !in_ty.is_simd() {
tcx.sess.emit_err(InvalidMonomorphization::SimdFirstType { span, name, in_ty });
return Err(());
}
if !arg_tys[1].is_simd() {
tcx.sess.emit_err(InvalidMonomorphization::SimdSecondType {
span,
name,
in_ty: arg_tys[1],
});
return Err(());
}
if !arg_tys[2].is_simd() {
tcx.sess.emit_err(InvalidMonomorphization::SimdThirdType {
span,
name,
in_ty: arg_tys[2],
});
return Err(());
}
if !ret_ty.is_simd() {
tcx.sess.emit_err(InvalidMonomorphization::SimdReturnType {
span,
name,
in_ty: ret_ty,
});
return Err(());
}

// Of the same length:
let (out_len, _) = arg_tys[1].simd_size_and_type(bx.tcx());
let (out_len2, _) = arg_tys[2].simd_size_and_type(bx.tcx());
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_codegen_ssa/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,4 +635,12 @@ pub enum InvalidMonomorphization<'tcx> {
span: Span,
name: Symbol,
},

#[diag(codegen_ssa_invalid_monomorphization_simd_argument, code = "E0511")]
SimdArgument {
#[primary_span]
span: Span,
name: Symbol,
ty: Ty<'tcx>,
},
}
2 changes: 2 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,5 @@ codegen_ssa_invalid_monomorphization_floating_point_vector = invalid monomorphiz
codegen_ssa_invalid_monomorphization_floating_point_type = invalid monomorphization of `{$name}` intrinsic: `{$in_ty}` is not a floating-point type
codegen_ssa_invalid_monomorphization_unrecognized_intrinsic = invalid monomorphization of `{$name}` intrinsic: unrecognized intrinsic `{$name}`
codegen_ssa_invalid_monomorphization_simd_argument = invalid monomorphization of `{$name}` intrinsic: expected SIMD argument type, found non-SIMD `{$ty}`

0 comments on commit 64aee83

Please sign in to comment.