Skip to content

Commit

Permalink
[Stdlib] Enable compile time hashing
Browse files Browse the repository at this point in the history
The only thing that was stopping us from performing compile time hashing
was the use of llvm.memset which is not necessary. Replace with a more
efficient vectorized memset and add a compile time hashing test.

MODULAR_ORIG_COMMIT_REV_ID: 330c93b84aa01aed33cdccbc8624ef5862f4a3da
abduld authored and modularbot committed Sep 1, 2024
1 parent d2701d9 commit bc202fa
Showing 2 changed files with 19 additions and 5 deletions.
15 changes: 10 additions & 5 deletions stdlib/src/memory/memory.mojo
Original file line number Diff line number Diff line change
@@ -276,12 +276,17 @@ fn memcpy(dest: UnsafePointer, src: __type_of(dest), count: Int):


@always_inline("nodebug")
fn _memset_llvm[
fn _memset_impl[
address_space: AddressSpace
](ptr: UnsafePointer[UInt8, address_space], value: UInt8, count: Int):
llvm_intrinsic["llvm.memset", NoneType](
ptr.address, value, count.value, False
)
alias simd_width = simdwidthof[UInt8]()
var vector_end = _align_down(count, simd_width)

for i in range(0, vector_end, simd_width):
ptr.store(i, SIMD[DType.uint8, simd_width](value))

for i in range(vector_end, count):
ptr.store(i, value)


@always_inline
@@ -299,7 +304,7 @@ fn memset[
value: The value to fill with.
count: Number of elements to fill (in elements, not bytes).
"""
_memset_llvm(ptr.bitcast[UInt8](), value, count * sizeof[type]())
_memset_impl(ptr.bitcast[UInt8](), value, count * sizeof[type]())


# ===----------------------------------------------------------------------===#
9 changes: 9 additions & 0 deletions stdlib/test/builtin/test_hash.mojo
Original file line number Diff line number Diff line change
@@ -138,7 +138,16 @@ fn test_issue_31111():
_ = hash(Int(1))


def test_hash_comptime():
alias hash_123 = hash("123")
assert_equal(hash_123, hash("123"))

alias hash_22 = hash(22)
assert_equal(hash_22, hash(22))


def main():
test_hash_byte_array()
test_hash_simd()
test_issue_31111()
test_hash_comptime()

0 comments on commit bc202fa

Please sign in to comment.