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

[SR-5352] Need a better way to do low-level bit-packing #47926

Closed
dabrahams opened this issue Jul 2, 2017 · 13 comments
Closed

[SR-5352] Need a better way to do low-level bit-packing #47926

dabrahams opened this issue Jul 2, 2017 · 13 comments
Assignees
Labels
affects ABI Flag: Affects ABI bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself SILGen Area → compiler: The SIL generation stage

Comments

@dabrahams
Copy link
Collaborator

Previous ID SR-5352
Radar rdar://problem/33096571
Original Reporter @dabrahams
Type Bug
Status Closed
Resolution Duplicate
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, AffectsABI, SILGen
Assignee @dabrahams
Priority Medium

md5: b3855e05ffd7f05a5d30c2dc3b3b10f2

Issue Description:

swift -parse-stdlib the following:

import Swift
print(MemoryLayout<(Builtin.Int120, UInt8)>.size * 8) // 136, should be 128
print(MemoryLayout<Builtin.Int120>.size * 8)              // 128, should be 120

This makes it extremely difficult to do low-level optimization

@dabrahams
Copy link
Collaborator Author

@swift-ci create

@jckarter
Copy link
Member

jckarter commented Jul 5, 2017

This is IRGen's purview. We don't currently do bit-packing for tuples or structs. It's unlikely we'd ever be able to bit-pack tuples due to abstraction limitations (our runtime value witness tables can't handle overlapping layout, and someone might use (Int120, Int8) as an unspecialized `(T, U)`), but a struct might one day be able to overlap fields like this.

@jckarter
Copy link
Member

jckarter commented Jul 5, 2017

SR-3723 looks like it covers efficient struct layout.

@dabrahams
Copy link
Collaborator Author

Well, I wasn't really after tuples; that's just how I demonstrated the issue. I actually wanted it to pack in a struct, and the alternative requires resorting to really dangerous `unsafeBitCast`s.

Also, why do you call this overlapping?

@jckarter
Copy link
Member

jckarter commented Jul 5, 2017

It's overlapping because Int120 is going to be treated as having 128 bits of 32- or 64-bit-aligned storage, and the Int8 would be superimposed into unused space in that representation.

@dabrahams
Copy link
Collaborator Author

That sort of begs the question. Why is Int120 going to be treated as having 128 bits? That doesn't seem obvious at all.

Note that this works:

struct X { var a: (Int64,Int32,Int16,Int8), b: UInt8 }
assert(MemoryLayout<X>.size * 8 == 128)

@jckarter
Copy link
Member

jckarter commented Jul 6, 2017

The unhelpful glib answer would be "because LLVM says so". If you were going to do arithmetic on Int120 and not just use it as bit storage, then it's going to be more efficient to do so in register-sized chunks without having to mind the odd-sized final chunk.

@dabrahams
Copy link
Collaborator Author

Thanks for the unhelpful glib answer, my friend!

Of course I would extend the thing to Int128 when doing any arithmetic, though. The point of using something like Builtin.Int120 is to control storage.

@jckarter
Copy link
Member

jckarter commented Jul 6, 2017

Well, the original purpose was only to have something to map to the underlying llvm integer types. If you want to control storage, it seems to me you probably also want to control alignment somehow, and integer builtins aren't great for that. Maybe we could have a `Builtin.AlignedStorageSS_AA` to summon storage of size SS/alignment AA.

@dabrahams
Copy link
Collaborator Author

That would be great, but I'd still need a reasonable way to get bits out of it, so it had better be zext-able to some Int type at least. Or maybe just Builtin.reinterpretCast-able to an IntSS.

@dabrahams
Copy link
Collaborator Author

Seems there's still some life in this discussion

@bob-wilson
Copy link

I discussed this today with a few people and we agreed that if we want to add support for low-level bit packing in the future, it would not use the Builtin.Int* types.

@dabrahams
Copy link
Collaborator Author

I don't see how "we wouldn't using Builtin.Int* to do low-level bit packing" implies "we won't do a better way to do low-level bit-packing"

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects ABI Flag: Affects ABI bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself SILGen Area → compiler: The SIL generation stage
Projects
None yet
Development

No branches or pull requests

3 participants