-
Notifications
You must be signed in to change notification settings - Fork 20
feat(riscv): basic datalayout support #1231
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 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,73 @@ | ||
| module | ||
|
|
||
| public import Veir.Interfaces.DataLayoutInterfaces | ||
|
|
||
| /-! | ||
| # RV64 Data Layout | ||
|
|
||
| The fixed data layout used by the RV64 backend. | ||
| -/ | ||
|
|
||
| namespace Veir.DataLayout | ||
|
|
||
| /-- Round `size` up to a positive byte alignment. -/ | ||
| private def alignTo (size alignment : Nat) : Nat := | ||
| if alignment = 0 then size | ||
| else ((size + alignment - 1) / alignment) * alignment | ||
|
|
||
| /-- The smallest power of two greater than or equal to `n` (and `1` for `0`). -/ | ||
| private def powerOfTwoCeil (n : Nat) : Nat := | ||
| if n ≤ 1 then 1 else 2 ^ (Nat.log2 (n - 1) + 1) | ||
|
|
||
| /-- | ||
| RV64 integer ABI alignment, derived from LLVM's | ||
| `i1:8-i8:8-i16:16-i32:32-i64:64-i128:128` layout entries. As in LLVM and | ||
| MLIR, an unlisted width uses the next larger entry, or the largest entry when | ||
| no larger one exists. | ||
| -/ | ||
| private def rv64IntegerAlignment (bitwidth : Nat) : Nat := | ||
| if bitwidth ≤ 8 then 1 | ||
| else if bitwidth ≤ 16 then 2 | ||
| else if bitwidth ≤ 32 then 4 | ||
| else if bitwidth ≤ 64 then 8 | ||
| else 16 | ||
|
|
||
| private def scalarInfo (size alignment : Nat) : DataLayoutTypeInfo := | ||
| { size | ||
| abiAlignment := alignment | ||
| preferredAlignment := alignment } | ||
|
|
||
| /-- Layout facts for the LLVM-compatible fixed-size types supported by VeIR. -/ | ||
| private def queryRISCV64 (type : Attribute) : Option DataLayoutTypeInfo := | ||
| match type with | ||
| | .integerType { bitwidth } | .byteType { bitwidth } => | ||
| if bitwidth = 0 then none | ||
| else | ||
| let size := (bitwidth + 7) / 8 | ||
| some (scalarInfo size (rv64IntegerAlignment bitwidth)) | ||
| | .floatType { bitwidth } => | ||
| if bitwidth = 0 then none | ||
| else | ||
| let size := (bitwidth + 7) / 8 | ||
| some (scalarInfo size (powerOfTwoCeil size)) | ||
| | .llvmPointerType _ => | ||
| some (scalarInfo 8 8) | ||
| | .llvmArrayType { size, type } => do | ||
| let element ← queryRISCV64 type | ||
| let elementAllocSize := alignTo element.size element.abiAlignment | ||
| let arraySize := elementAllocSize * size | ||
| some | ||
| { size := arraySize | ||
| abiAlignment := element.abiAlignment | ||
| preferredAlignment := element.preferredAlignment } | ||
| | _ => none | ||
|
|
||
| /-- | ||
| The standard RV64 data layout: | ||
| `e-m:e-p:64:64-i64:64-i128:128-n32:64-S128`, together with LLVM's | ||
| default primitive entries. | ||
| -/ | ||
| public def riscv64 : DataLayout := | ||
| { query := queryRISCV64 } | ||
|
|
||
| end Veir.DataLayout | ||
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,66 @@ | ||
| module | ||
|
|
||
| public import Veir.IR.Attribute | ||
|
|
||
| /-! | ||
| # DataLayoutInterface | ||
|
|
||
| Target data layouts answer physical representation queries for IR types. The | ||
| interface deliberately distinguishes the byte size of a type, its ABI and | ||
| preferred alignments, and its allocation size (the stride between consecutive | ||
| objects). In particular, an odd-width integer can have a three-byte type size | ||
|
regehr marked this conversation as resolved.
Outdated
|
||
| but a four-byte allocation size. | ||
| -/ | ||
|
|
||
| namespace Veir | ||
|
|
||
| public section | ||
|
|
||
| /-- The fixed-size layout facts for one type, all expressed in bytes. -/ | ||
| structure DataLayoutTypeInfo where | ||
| size : Nat | ||
| abiAlignment : Nat | ||
| preferredAlignment : Nat | ||
| deriving Inhabited, Repr, DecidableEq | ||
|
|
||
| /-- | ||
| A target data layout. Unsupported or unsized types return `none`. | ||
|
|
||
| Keeping the query behind an object lets passes depend on the interface rather | ||
| than on how layout information is obtained (currently fixed RV64 values, | ||
| eventually perhaps parsed DLTI entries). | ||
| -/ | ||
| structure DataLayout where | ||
| query : Attribute → Option DataLayoutTypeInfo | ||
|
|
||
| namespace DataLayout | ||
|
|
||
| /-- Round `size` up to a positive byte alignment. -/ | ||
| private def alignTo (size alignment : Nat) : Nat := | ||
|
regehr marked this conversation as resolved.
Outdated
|
||
| if alignment = 0 then size | ||
| else ((size + alignment - 1) / alignment) * alignment | ||
|
|
||
| /-- Return the size of `type` in bytes, including padding internal to the type. -/ | ||
| def getTypeSize (layout : DataLayout) (type : Attribute) : Option Nat := | ||
| (layout.query type).map (·.size) | ||
|
|
||
| /-- Return the minimum ABI-required alignment of `type`, in bytes. -/ | ||
| def getTypeABIAlignment (layout : DataLayout) (type : Attribute) : Option Nat := | ||
| (layout.query type).map (·.abiAlignment) | ||
|
|
||
| /-- Return the preferred alignment of `type`, in bytes. -/ | ||
| def getTypePreferredAlignment (layout : DataLayout) (type : Attribute) : Option Nat := | ||
| (layout.query type).map (·.preferredAlignment) | ||
|
|
||
| /-- | ||
| Return the allocation size of `type`, in bytes: the stride between consecutive | ||
| objects, including tail padding required by the ABI alignment. | ||
| -/ | ||
| def getTypeAllocSize (layout : DataLayout) (type : Attribute) : Option Nat := | ||
| (layout.query type).map fun info => alignTo info.size info.abiAlignment | ||
|
|
||
| end DataLayout | ||
|
|
||
| end | ||
|
|
||
| end Veir | ||
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.