Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 6 additions & 29 deletions kani-compiler/src/codegen_cprover_gotoc/codegen/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,35 +256,12 @@ impl GotocCtx<'_, '_> {

let intrinsic = Intrinsic::from_instance(&instance);

// A SIMD intrinsic operates on SIMD vectors: `simd_splat` returns one, the others take one
// as their first argument. rustc rejects any other instantiation, but autoharness can
// instantiate a generic function with a scalar (e.g. stdarch's `simd_imin::<T>` with
// `T = i32`), so report it as unsupported rather than panic in the codegen below.
let simd_ty = match &intrinsic {
Intrinsic::SimdSplat => Some(ret_ty),
Intrinsic::SimdAdd
| Intrinsic::SimdAnd
| Intrinsic::SimdDiv
| Intrinsic::SimdReduceAll
| Intrinsic::SimdRem
| Intrinsic::SimdEq
| Intrinsic::SimdExtract
| Intrinsic::SimdGe
| Intrinsic::SimdGt
| Intrinsic::SimdInsert
| Intrinsic::SimdLe
| Intrinsic::SimdLt
| Intrinsic::SimdMul
| Intrinsic::SimdNe
| Intrinsic::SimdOr
| Intrinsic::SimdShl
| Intrinsic::SimdShr
| Intrinsic::SimdShuffle(_)
| Intrinsic::SimdSub
| Intrinsic::SimdXor => farg_types.first().copied(),
_ => None,
};
if let Some(ty) = simd_ty
// A SIMD intrinsic operates on SIMD vectors (see `Intrinsic::simd_vector_operand`). rustc's
// codegen backends reject any other instantiation, but Kani replaces those backends, so a
// scalar can reach this point (e.g. autoharness instantiating stdarch's `simd_imin::<T>`
// with `T = i32`, or a harness calling such a helper directly). Report it as unsupported
// rather than panic in the codegen below.
if let Some(ty) = intrinsic.simd_vector_operand(farg_types, ret_ty)
&& !ty.kind().is_simd()
{
return self.codegen_unimplemented_stmt(
Expand Down
43 changes: 42 additions & 1 deletion kani-compiler/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use rustc_public::{
mir::{Mutability, mono::Instance},
ty::{FloatTy, IntTy, RigidTy, TyKind, UintTy},
ty::{FloatTy, IntTy, RigidTy, Ty, TyKind, UintTy},
};

// Enumeration of all intrinsics we support right now, with the last option being a catch-all. This
Expand Down Expand Up @@ -176,6 +176,47 @@ macro_rules! assert_sig_matches {
}

impl Intrinsic {
/// The operand that must be a SIMD vector for this intrinsic to have a meaning, given the
/// monomorphized argument and return types of a call to it; `None` for a non-SIMD intrinsic.
///
/// rustc's codegen backends reject a violation with `E0511` (`invalid monomorphization of
/// `simd_lt` intrinsic: expected SIMD input type, found non-SIMD `i32``). Kani replaces the
/// backend, so it sees such calls: from a harness that calls `imin::<i32>` directly, and from
/// instantiations that `-Z autoharness` picks for a helper like
/// `fn imin<T: Copy>(a: T, b: T) -> T` whose body calls `simd_lt`, since nothing bounds `T` to
/// the SIMD types. See <https://github.com/model-checking/kani/issues/4919>.
pub fn simd_vector_operand(&self, arg_tys: &[Ty], ret_ty: Ty) -> Option<Ty> {
match self {
// `simd_splat<T, U>(value: U) -> T` broadcasts a scalar, so its vector is the return
// type. Every other SIMD intrinsic takes a vector first, including the ones that take
// a scalar or an index later (`simd_insert`, `simd_extract`, `simd_shuffle`).
Intrinsic::SimdSplat => Some(ret_ty),
Intrinsic::SimdAdd
| Intrinsic::SimdAnd
| Intrinsic::SimdDiv
| Intrinsic::SimdEq
| Intrinsic::SimdExtract
| Intrinsic::SimdGe
| Intrinsic::SimdGt
| Intrinsic::SimdInsert
| Intrinsic::SimdLe
| Intrinsic::SimdLt
| Intrinsic::SimdMul
| Intrinsic::SimdNe
| Intrinsic::SimdOr
| Intrinsic::SimdReduceAll
| Intrinsic::SimdRem
| Intrinsic::SimdShl
| Intrinsic::SimdShr
| Intrinsic::SimdShuffle(_)
| Intrinsic::SimdSub
| Intrinsic::SimdXor => arg_tys.first().copied(),
// Listing the non-SIMD intrinsics would not make a new SIMD one any harder to miss:
// `try_match_simd` is where they are added, and it sits next to this match.
_ => None,
}
}

/// Create an intrinsic enum from a given intrinsic instance, shallowly validating the argument types.
pub fn from_instance(intrinsic_instance: &Instance) -> Self {
let intrinsic_str = intrinsic_instance.intrinsic_name().unwrap();
Expand Down
59 changes: 58 additions & 1 deletion kani-compiler/src/kani_middle/codegen_units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! according to their stub configuration.

use crate::args::{Arguments, ReachabilityType};
use crate::intrinsics::Intrinsic;
use crate::kani_middle::attributes::{KaniAttributes, is_proof_harness};
use crate::kani_middle::kani_functions::{KaniHook, KaniIntrinsic, KaniModel};
use crate::kani_middle::metadata::{
Expand All @@ -29,6 +30,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def_id::DefId;
use rustc_infer::infer::TyCtxtInferExt;
use rustc_middle::ty::{self, TyCtxt, TypingMode};
use rustc_public::mir::TerminatorKind;
use rustc_public::mir::mono::Instance;
use rustc_public::rustc_internal;
use rustc_public::ty::{
Expand Down Expand Up @@ -897,6 +899,42 @@ fn resolve_deferred_fn_slots<'tcx>(
true
}

/// The SIMD intrinsic call in `instance`'s body whose vector operand this instantiation turned
/// into a non-SIMD type, if there is one, as `(intrinsic name, operand type)`.
///
/// rustc's codegen backends reject such a monomorphization with `E0511`, so the call site cannot
/// appear in a program that `cargo build` accepts: nothing bounds the parameter of a helper like
/// `fn imin<T: Copy>(a: T, b: T) -> T { simd_lt(a, b) }` to the SIMD types, but every real caller
/// passes one. Autoharness picks its own instantiation, though, so it has to make that check
/// itself, or the harness it generates is one rustc would not accept. `core` has exactly this
/// shape in `core_arch::simd::simd_imin`, which is what blocked autoharness over the standard
/// library: <https://github.com/model-checking/kani/issues/4919>.
///
/// Only `instance`'s own body is inspected. An invalid instantiation a level down (a generic
/// function calling a generic SIMD helper) is reported by codegen against the harness that reached
/// it, which fails that harness instead of the run; see
/// <https://github.com/model-checking/kani/issues/4926>.
fn invalid_simd_instantiation(instance: Instance) -> Option<(String, Ty)> {
let body = instance.body()?;
body.blocks.iter().find_map(|block| {
let TerminatorKind::Call { func, .. } = &block.terminator.kind else {
return None;
};
let TyKind::RigidTy(RigidTy::FnDef(def, args)) = func.ty(body.locals()).ok()?.kind() else {
return None;
};
let callee = Instance::resolve(def, &args).ok()?;
// `try_match_simd` recognizes exactly the intrinsics named `simd_*`, so do not ask about
// any other: the `Intrinsic` conversion asserts on the signatures it knows, and selecting
// a harness should not be able to trip an assertion that codegen would not.
let name = callee.intrinsic_name().filter(|name| name.starts_with("simd_"))?;
let sig = callee.ty().kind().fn_sig()?.skip_binder();
let operand_ty =
Intrinsic::from_instance(&callee).simd_vector_operand(sig.inputs(), sig.output())?;
(!operand_ty.kind().is_simd()).then_some((name, operand_ty))
})
}

/// Try to find a monomorphic instantiation of the generic function `fn_item` for which we can
/// generate an automatic harness. Substitute each type parameter with the first candidate from
/// `generic_instantiation_candidates` such that all of the function's trait bounds are satisfied
Expand Down Expand Up @@ -997,6 +1035,9 @@ fn choose_generic_instantiation(
};

let attempts = std::cell::Cell::new(0usize);
// The first candidate rejected for making a SIMD intrinsic call invalid, so that the skip
// reason names that instead of the trait bounds, which such a candidate does satisfy.
let simd_rejection: std::cell::Cell<Option<String>> = std::cell::Cell::new(None);
let try_choice = |choice: &[Ty]| -> Option<Instance> {
attempts.set(attempts.get() + 1);
let args = build_args(choice);
Expand All @@ -1007,7 +1048,18 @@ fn choose_generic_instantiation(
// instance (e.g. a generic trait method without a default) is then reported accurately
// as `NoBody` by `skip_reason`, rather than falling through to a generic-function skip
// reason here.
Instance::resolve(def, &args).ok()
let instance = Instance::resolve(def, &args).ok()?;
if let Some((name, operand_ty)) = invalid_simd_instantiation(instance) {
let reason = simd_rejection.take().unwrap_or_else(|| {
format!(
"the body calls the SIMD intrinsic `{name}`, which rustc rejects for the \
non-SIMD type `{operand_ty}` this instantiation gives it"
)
});
simd_rejection.set(Some(reason));
return None;
}
Some(instance)
};

// First pass: the same primitive candidate for every type parameter (the common case,
Expand Down Expand Up @@ -1066,6 +1118,11 @@ fn choose_generic_instantiation(
{
return Ok(instance);
}
// A candidate that satisfied the bounds but would make a SIMD intrinsic call invalid is the
// more specific reason, so report it ahead of the generic ones below.
if let Some(reason) = simd_rejection.take() {
return Err(reason);
}
if search_limited {
return Err(format!(
"generic instantiation search reached the limit of \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
# Copyright Kani Contributors
# SPDX-License-Identifier: Apache-2.0 OR MIT

set -eu

# A negated `grep` is exempt from `errexit`, so spell out the absent-pattern check.
absent() {
if grep -qE "$1" out.log; then
echo "unexpectedly found '$1'"
exit 1
fi
}

kani autoharness -Z autoharness simd_generic.rs --output-format=terse > out.log 2>&1 || true

echo "--- the reproducer is instantiated with the SIMD type, not with a scalar"
grep -qE '^\| simd_generic +\| imin::<I32x2>' out.log
absent '^\| simd_generic +\| imin::<i32>'

echo "--- reaching the invalid instantiation through a call reports it, rather than crashing"
grep -q '`simd_lt` on non-SIMD type `i32` is not currently supported' out.log
grep -q 'in imin::<i32>' out.log
grep -qE '^\| simd_generic +\| calls_imin::<i32> .*Failure' out.log

echo "--- a function whose SIMD operand types are fixed is unaffected"
grep -qE '^\| simd_generic +\| compare_vectors .*Success' out.log

echo "--- and the run finishes, rather than aborting the compiler"
grep -q 'Complete - 3 successfully verified functions, 1 failures, 4 total' out.log

rm -f out.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright Kani Contributors
# SPDX-License-Identifier: Apache-2.0 OR MIT
script: check.sh
expected: expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--- the reproducer is instantiated with the SIMD type, not with a scalar
--- reaching the invalid instantiation through a call reports it, rather than crashing
--- a function whose SIMD operand types are fixed is unaffected
--- and the run finishes, rather than aborting the compiler
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! A generic helper whose body calls a SIMD intrinsic, but whose type parameter is not bound to
//! the SIMD types. rustc's codegen backends reject `imin::<i32>` (`E0511`), so no program that
//! `cargo build` accepts contains that call; autoharness synthesizes its own instantiation, though, and
//! used to pick `i32` and crash the SIMD codegen on the vector length of a scalar:
//! <https://github.com/model-checking/kani/issues/4919>. `core_arch::simd::simd_imin` has exactly
//! this shape, which is what blocked autoharness over the standard library.
//!
//! Autoharness now rejects an instantiation that would make the call invalid, so it picks the
//! `#[repr(simd)]` type instead. A caller that makes the invalid instantiation one level down is
//! only caught in codegen, and fails its own harness rather than the run.

#![feature(core_intrinsics, repr_simd)]
#![allow(internal_features)]

use std::intrinsics::simd::{simd_lt, simd_xor};

#[repr(simd)]
#[derive(Copy, Clone)]
pub struct I32x2([i32; 2]);

/// The reproducer: `T` only has to be `Copy`, so `i32` satisfies the bounds but not `simd_lt`.
pub unsafe fn imin<T: Copy>(a: T, b: T) -> T {
let mask: T = simd_lt(a, b);
// Kani does not model `simd_select`; combine the lanes with an intrinsic it does model and
// that cannot overflow, since the point here is which type `T` is instantiated with.
simd_xor(mask, b)
}

/// Reaches the invalid instantiation through a call, which autoharness does not inspect: its own
/// body calls no SIMD intrinsic, so `i32` is chosen and codegen reports the unsupported construct.
/// Detecting this in the harness selection instead is <https://github.com/model-checking/kani/issues/4926>.
pub unsafe fn calls_imin<T: Copy>(a: T, b: T) -> T {
imin(a, b)
}

/// A SIMD intrinsic whose operand types are fixed: unaffected by the check.
pub unsafe fn compare_vectors(a: I32x2, b: I32x2) -> I32x2 {
simd_lt(a, b)
}
Loading