-
Notifications
You must be signed in to change notification settings - Fork 70
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
documentation: (toy) add initial lowering to riscv #1365
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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,41 @@ | ||
from xdsl.dialects import riscv | ||
from xdsl.dialects.builtin import ModuleOp | ||
from xdsl.ir.core import Block, MLContext, Region | ||
from xdsl.passes import ModulePass | ||
from xdsl.pattern_rewriter import ( | ||
PatternRewriter, | ||
PatternRewriteWalker, | ||
RewritePattern, | ||
op_type_rewrite_pattern, | ||
) | ||
|
||
|
||
class AddSections(RewritePattern): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With @AntonLydike's riscemu malloc and friends, the .bss section hack is not necessary, similarly we can probably get away with dropping the data section. I'd like to first merge the end-to-end flow as it's working today, and make it better later, if that's ok with everyone. |
||
@op_type_rewrite_pattern | ||
def match_and_rewrite(self, op: ModuleOp, rewriter: PatternRewriter): | ||
# bss stands for block starting symbol | ||
heap_section = riscv.DirectiveOp( | ||
".bss", | ||
None, | ||
Region( | ||
Block( | ||
[ | ||
riscv.LabelOp("heap"), | ||
riscv.DirectiveOp(".space", f"{1024}"), # 1kb | ||
] | ||
) | ||
), | ||
) | ||
data_section = riscv.DirectiveOp(".data", None, Region(Block())) | ||
text_section = riscv.DirectiveOp( | ||
".text", None, rewriter.move_region_contents_to_new_regions(op.regions[0]) | ||
) | ||
|
||
op.body.add_block(Block([heap_section, data_section, text_section])) | ||
|
||
|
||
class SetupRiscvPass(ModulePass): | ||
name = "setup-lowering-to-riscv" | ||
|
||
def apply(self, ctx: MLContext, op: ModuleOp) -> None: | ||
PatternRewriteWalker(AddSections()).rewrite_module(op) |
This file contains 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,26 @@ | ||
from xdsl.builder import ImplicitBuilder | ||
from xdsl.dialects.builtin import ModuleOp | ||
from xdsl.dialects.func import FuncOp | ||
from xdsl.dialects.riscv import DirectiveOp, LabelOp | ||
from xdsl.ir import Block, MLContext, Region | ||
|
||
from ..rewrites.setup_riscv_pass import SetupRiscvPass | ||
|
||
with ImplicitBuilder((input_module := ModuleOp([])).body): | ||
FuncOp("main", ((), ())) | ||
|
||
with ImplicitBuilder((output_module := ModuleOp([])).body): | ||
bss = DirectiveOp(".bss", None, Region(Block())) | ||
with ImplicitBuilder(bss.data): | ||
LabelOp("heap") | ||
DirectiveOp(".space", f"{1024}") | ||
data = DirectiveOp(".data", None, Region(Block())) | ||
text = DirectiveOp(".text", None, Region(Block())) | ||
with ImplicitBuilder(text.data): | ||
FuncOp("main", ((), ())) | ||
|
||
|
||
def test_add_sections(): | ||
module = input_module.clone() | ||
SetupRiscvPass().apply(MLContext(), module) | ||
assert f"{module}" == f"{output_module}" |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
twice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes I guess not really necessary right now, I'll remove it in the next PR