Skip to content
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

[stdlib] Add SIMD debug_assert(value < max_unsigned_value) for Int constructors #3761

Draft
wants to merge 6 commits into
base: nightly
Choose a base branch
from
Draft
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
14 changes: 13 additions & 1 deletion stdlib/src/builtin/simd.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,18 @@ struct SIMD[type: DType, size: Int](
fn __init__(inout self, value: __mlir_type.index):
_simd_construction_checks[type, size]()

@parameter
if type is not DType.index:
alias max_unsigned_value = 2 ** bitwidthof[type]()
debug_assert[assert_mode="safe", cpu_only=True](
UInt(value) < UInt(max_unsigned_value),
"Overflow on ",
type,
" construction. Maximum unsigned value for this DType is ",
UInt(max_unsigned_value) - 1,
". Construct a `SIMD[DType.index, _]` if you are sure.",
)

var t0 = __mlir_op.`pop.cast_from_builtin`[
_type = __mlir_type.`!pop.scalar<index>`
](value)
Expand Down Expand Up @@ -2100,7 +2112,7 @@ struct SIMD[type: DType, size: Int](
"llvm.vector.extract",
SIMD[type, output_width],
has_side_effect=False,
](self, Int64(offset))
](self, Scalar[DType.index](offset).cast[DType.uint64]())

@always_inline("nodebug")
fn insert[*, offset: Int = 0](self, value: SIMD[type, _]) -> Self:
Expand Down
Loading