Skip to content

aarch64: Migrate {s,u}{div,rem} to ISLE#3572

Merged
alexcrichton merged 2 commits intobytecodealliance:mainfrom
alexcrichton:isle-4-sdiv-udiv-srem-urem
Dec 13, 2021
Merged

aarch64: Migrate {s,u}{div,rem} to ISLE#3572
alexcrichton merged 2 commits intobytecodealliance:mainfrom
alexcrichton:isle-4-sdiv-udiv-srem-urem

Conversation

@alexcrichton
Copy link
Copy Markdown
Member

This commit migrates four different instructions at once to ISLE:

  • sdiv
  • udiv
  • srem
  • urem

These all share similar codegen and center around the div instruction
to use internally. The main feature of these was to model the manual
traps since the div instruction doesn't trap on overflow, instead
requiring manual checks to adhere to the semantics of the instruction
itself.

While I was here I went ahead and implemented an optimization for these
instructions when the right-hand-side is a constant with a known value.
For udiv, srem, and urem if the right-hand-side is a nonzero
constant then the checks for traps can be skipped entirely. For sdiv
if the constant is not 0 and not -1 then additionally all checks can be
elided. Finally if the right-hand-side of sdiv is -1 the zero-check is
elided, but it still needs a check for i64::MIN on the left-hand-side
and currently there's a TODO where -1 is still checked too.

(_2 Unit (emit (MInst.CCmpImm (size_from_ty ty)
x
(u8_into_uimm5 1)
(nzcv $false $false $false $false)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a drive-by comment (I'm happy to review the whole thing too but wanted to note this first): eek, a language wart appears! I had included #t and #f bool constants (Scheme-style) but they lower to 1 / 0 (i.e., integers); this is largely because the DSL compiler's IR has ConstInt but not ConstBool because... no good reason. I see here you've used the $ syntax to just pass through false as an opaque constant name; that works but we should really fix this.

(Outside the scope of this PR obviously, just noting -- I'll file an issue for it. Sorry about this!)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok! I'm happy to switch to whatever is the right way to do this, I think there's other places that stem from

(extern const $true bool)
(extern const $false bool)
which would need an update as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #3573 for this, happy to do the cleanup later; this bit can land as-is for now :-)

@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift:area:aarch64 Issues related to AArch64 backend. cranelift:area:x64 Issues related to x64 codegen isle Related to the ISLE domain-specific language labels Nov 30, 2021
@github-actions
Copy link
Copy Markdown

Subscribe to Label Action

cc @cfallin, @fitzgen

Details This issue or pull request has been labeled: "cranelift", "cranelift:area:aarch64", "cranelift:area:machinst", "cranelift:area:x64", "isle"

Thus the following users have been cc'd because of the following labels:

  • cfallin: isle
  • fitzgen: isle

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

@alexcrichton alexcrichton requested a review from fitzgen December 13, 2021 16:23
Copy link
Copy Markdown
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo a few comments below.

Comment on lines +231 to +254
fn cond_br_zero(&mut self, reg: Reg) -> CondBrKind {
CondBrKind::Zero(reg)
}

fn cond_br_cond(&mut self, cond: &Cond) -> CondBrKind {
CondBrKind::Cond(*cond)
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should CondBrKind be moved into ISLE? This seems like a lot of code to write to glue the non-ISLE definition in with the ISLE code. Doesn't need to happen in this PR (or at all) unless you want, but just something I though of when looking at this code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently CondBrKind is pretty heavily used it looks like in the backend and since ISLE can only generate named-field enums instead of tuple-enums like CondBrKind is today I think I'll leave it defined externally, but I agree this'd be a good future cleanup!

This commit migrates four different instructions at once to ISLE:

* `sdiv`
* `udiv`
* `srem`
* `urem`

These all share similar codegen and center around the `div` instruction
to use internally. The main feature of these was to model the manual
traps since the `div` instruction doesn't trap on overflow, instead
requiring manual checks to adhere to the semantics of the instruction
itself.

While I was here I went ahead and implemented an optimization for these
instructions when the right-hand-side is a constant with a known value.
For `udiv`, `srem`, and `urem` if the right-hand-side is a nonzero
constant then the checks for traps can be skipped entirely. For `sdiv`
if the constant is not 0 and not -1 then additionally all checks can be
elided. Finally if the right-hand-side of `sdiv` is -1 the zero-check is
elided, but it still needs a check for `i64::MIN` on the left-hand-side
and currently there's a TODO where `-1` is still checked too.
@alexcrichton alexcrichton force-pushed the isle-4-sdiv-udiv-srem-urem branch from c7034ea to 83b6108 Compare December 13, 2021 22:38
@alexcrichton alexcrichton merged commit 20e090b into bytecodealliance:main Dec 13, 2021
@alexcrichton alexcrichton deleted the isle-4-sdiv-udiv-srem-urem branch December 13, 2021 23:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift:area:aarch64 Issues related to AArch64 backend. cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift:area:x64 Issues related to x64 codegen cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants