-
Notifications
You must be signed in to change notification settings - Fork 116
Add support to simd_bitmask
#2677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c397d37
Add support to `simd_bitmask`
celinval 0787b12
Add hash_set perf test
celinval 9cd4185
Fix std library regression
celinval 78c9e50
Merge remote-tracking branch 'origin/main' into issue-2131-bitmask
celinval 9f05d8a
Apply suggestions from code review
celinval 724d163
Address comments
celinval 10554e6
Fix override check
celinval fcd1b10
Apply suggestions from code review
celinval 0083a27
Improve the transformation code
celinval f53547b
Merge branch 'main' into issue-2131-bitmask
celinval File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright Kani Contributors | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
//! This module contains a MIR pass that replaces some intrinsics by rust intrinsics models as | ||
//! well as validation logic that can only be added during monomorphization. | ||
use rustc_index::IndexVec; | ||
use rustc_middle::mir::{interpret::ConstValue, Body, ConstantKind, Operand, TerminatorKind}; | ||
use rustc_middle::mir::{Local, LocalDecl}; | ||
use rustc_middle::ty::{self, Ty, TyCtxt}; | ||
use rustc_middle::ty::{Const, GenericArgsRef}; | ||
use rustc_span::symbol::{sym, Symbol}; | ||
use tracing::{debug, trace}; | ||
|
||
pub struct ModelIntrinsics<'tcx> { | ||
tcx: TyCtxt<'tcx>, | ||
/// Local declarations of the function being transformed. | ||
local_decls: IndexVec<Local, LocalDecl<'tcx>>, | ||
} | ||
|
||
impl<'tcx> ModelIntrinsics<'tcx> { | ||
/// Function that replace calls to some intrinsics that have a high level model in our library. | ||
/// | ||
/// For now, we only look at intrinsic calls, which are modelled by a terminator. | ||
/// | ||
/// However, this pass runs after lowering intrinsics, which may replace the terminator by | ||
/// an intrinsic statement (non-diverging intrinsic). | ||
pub fn run_pass(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { | ||
ModelIntrinsics { tcx, local_decls: body.local_decls.clone() }.transform(body) | ||
} | ||
|
||
pub fn transform(&self, body: &mut Body<'tcx>) { | ||
for block in body.basic_blocks.as_mut() { | ||
let terminator = block.terminator_mut(); | ||
if let TerminatorKind::Call { func, args, .. } = &mut terminator.kind { | ||
let func_ty = func.ty(&self.local_decls, self.tcx); | ||
if let Some((intrinsic_name, generics)) = resolve_rust_intrinsic(self.tcx, func_ty) | ||
{ | ||
trace!(?func, ?intrinsic_name, "run_pass"); | ||
if intrinsic_name == sym::simd_bitmask { | ||
self.replace_simd_bitmask(func, args, generics) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Change the function call to use the stubbed version. | ||
/// We only replace calls if we can ensure the input has simd representation. | ||
fn replace_simd_bitmask( | ||
&self, | ||
func: &mut Operand<'tcx>, | ||
args: &[Operand<'tcx>], | ||
gen_args: GenericArgsRef<'tcx>, | ||
) { | ||
assert_eq!(args.len(), 1); | ||
let tcx = self.tcx; | ||
let arg_ty = args[0].ty(&self.local_decls, tcx); | ||
if arg_ty.is_simd() { | ||
// Get the stub definition. | ||
let stub_id = tcx.get_diagnostic_item(Symbol::intern("KaniModelSimdBitmask")).unwrap(); | ||
debug!(?func, ?stub_id, "replace_simd_bitmask"); | ||
|
||
// Get SIMD information from the type. | ||
let (len, elem_ty) = simd_len_and_type(tcx, arg_ty); | ||
debug!(?len, ?elem_ty, "replace_simd_bitmask Ok"); | ||
|
||
// Increment the list of generic arguments since our stub also takes element type and len. | ||
let mut new_gen_args = Vec::from_iter(gen_args.iter()); | ||
new_gen_args.push(elem_ty.into()); | ||
new_gen_args.push(len.into()); | ||
|
||
let Operand::Constant(fn_def) = func else { unreachable!() }; | ||
fn_def.literal = ConstantKind::from_value( | ||
ConstValue::ZeroSized, | ||
tcx.type_of(stub_id).instantiate(tcx, &new_gen_args), | ||
); | ||
} else { | ||
debug!(?arg_ty, "replace_simd_bitmask failed"); | ||
JustusAdam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
fn simd_len_and_type<'tcx>(tcx: TyCtxt<'tcx>, simd_ty: Ty<'tcx>) -> (Const<'tcx>, Ty<'tcx>) { | ||
match simd_ty.kind() { | ||
ty::Adt(def, args) => { | ||
assert!(def.repr().simd(), "`simd_size_and_type` called on non-SIMD type"); | ||
let variant = def.non_enum_variant(); | ||
let f0_ty = variant.fields[0u32.into()].ty(tcx, args); | ||
|
||
match f0_ty.kind() { | ||
ty::Array(elem_ty, len) => (*len, *elem_ty), | ||
_ => (Const::from_target_usize(tcx, variant.fields.len() as u64), f0_ty), | ||
} | ||
} | ||
_ => unreachable!("unexpected layout for simd type {simd_ty}"), | ||
} | ||
} | ||
|
||
fn resolve_rust_intrinsic<'tcx>( | ||
tcx: TyCtxt<'tcx>, | ||
func_ty: Ty<'tcx>, | ||
) -> Option<(Symbol, GenericArgsRef<'tcx>)> { | ||
if let ty::FnDef(def_id, args) = *func_ty.kind() { | ||
if tcx.is_intrinsic(def_id) { | ||
return Some((tcx.item_name(def_id), args)); | ||
celinval marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
None | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.