modular icon indicating copy to clipboard operation
modular copied to clipboard

[stdlib] Add `DType.get_dtype()`

Open martinvuyk opened this issue 1 year ago • 3 comments

Add DType.get_dtype().

This will enable a lot of vector based optimizations for more generic function signatures.

This is a split-off from PR #3577

Examples:

fn memset[type: Copyable](ptr: UnsafePointer[type], value: type, count: Int):
    alias dt = DType.get_dtype[type]()

    @parameter
    if dt is not DType.invalid:
        var p = ptr.bitcast[Scalar[dt]]()
        _memset_impl[dt](p, rebind[Scalar[dt]](value), count)
    else:
        for i in range(count):
            (ptr + i).init_pointee_copy(value)

martinvuyk avatar Nov 25 '24 21:11 martinvuyk

The _from_str fixes seem "obviously good" to me, the extension I have less insight into though

lattner avatar Mar 30 '25 02:03 lattner

@JoeLoser now that I'm going through the stdlib searching for places where we should use UInt instead of Int I came across this:

        # TODO: Simplify this with a UInt type.
        debug_assert(
            -self._len <= Int(idx) < self._len, "index must be within bounds"
        )
        # TODO(MSTDL-1086): optimize away SIMD/UInt normalization check
        var offset = Int(idx)
        if offset < 0:
            offset += len(self)
        return self._data[offset]

I already changed it to:

        var offset = UInt(idx.__index__())
        # TODO(MSTDL-1086): optimize away SIMD normalization check
        if not _type_is_eq[I, UInt]():
            if Int(idx) < 0:
                offset = len(self) + Int(idx)
        debug_assert(offset < self._len, "index must be within bounds")
        return self._data[offset]

MSTDL-1086 would be solved by doing:

        var offset = UInt(idx.__index__())
        alias dt = DType.get_dtype[I]()

        @parameter
        if not _type_is_eq[I, UInt]() and not dt.is_unsigned():
            if Int(idx) < 0:
                offset = len(self) + Int(idx)
        debug_assert(offset < self._len, "index must be within bounds")
        return self._data[offset]

martinvuyk avatar Apr 06 '25 00:04 martinvuyk

!sync

JoeLoser avatar May 22 '25 23:05 JoeLoser

!sync

JoeLoser avatar Jun 07 '25 02:06 JoeLoser

✅🟣 This contribution has been merged 🟣✅

Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the main branch during the next Mojo nightly release, typically within the next 24-48 hours.

We use Copybara to merge external contributions, click here to learn more.

modularbot avatar Jun 10 '25 04:06 modularbot

Landed in cc194a9e44f703e1ac60ef1d09501640825c16ea! Thank you for your contribution 🎉

modularbot avatar Jun 10 '25 06:06 modularbot