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

No basic block terminator in a fall-thru #14

Merged
merged 5 commits into from
Apr 1, 2024

Conversation

jcivlin
Copy link

@jcivlin jcivlin commented Mar 24, 2024

Problem description

Stackless code may have basic block terminators removed by Move compiler optimizer (which is semantically ok for the fall-thru), whereas LLVM requires terminators in all basic blocks (even for the fall-thru).

Solution

This patch provides fixing of this problem and adds (restores) the omitted basic block terminator while translating
sbc::Bytecode::Label bytecode.

Example

For this test:

module 0x42::m {
    fun t4() {
        loop {}
    }
}

In the generated code the first br was absent and added in this patch:

define private void @"0000000000000042_m_t4_sZqr7puHcrnUk2"() {
entry:
  br label %bb_0

bb_0:                                             ; preds = %bb_0, %entry
  br label %bb_0
}

@jcivlin jcivlin changed the title No terminator in empty BB No basic block terminator in a fall-thru Mar 26, 2024
@jcivlin jcivlin force-pushed the 032424-failure-loop branch from 7e77f1b to e2d27ac Compare March 27, 2024 14:12
@jcivlin jcivlin force-pushed the 032424-failure-loop branch from e2d27ac to 4dbf8ea Compare March 27, 2024 14:54
@jcivlin jcivlin marked this pull request as ready for review March 27, 2024 16:03
@jcivlin jcivlin requested review from brson and ksolana March 27, 2024 16:04
let terminator = LLVMGetBasicBlockTerminator(prev_bb);
if terminator.is_null() {
LLVMPositionBuilderAtEnd(self.module_cx.llvm_builder.0, prev_bb);
LLVMBuildBr(self.module_cx.llvm_builder.0, bb);
Copy link
Collaborator

Choose a reason for hiding this comment

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

How do we know that LLVMGetPreviousBasicBlock gives a predecessor of bb? Even if this somehow works, this can lead to bug in future.

I think we should keep track of previous basic block in translate_instruction and pass that information to fix_terminator_in_prev_bb

Copy link
Author

@jcivlin jcivlin Apr 1, 2024

Choose a reason for hiding this comment

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

Yes, we know that LLVMGetPreviousBasicBlock gives the correct predecessor since it is how we build them in translate_instruction.
Providing an additional tracking will be just an unnecessary double booking.

@jcivlin jcivlin merged commit 610de11 into anza-xyz:solana Apr 1, 2024
12 checks passed
@jcivlin
Copy link
Author

jcivlin commented Apr 1, 2024

@ksolana thx for quick review!

@brson
Copy link
Collaborator

brson commented Apr 2, 2024

Thanks @jcivlin

ksolana pushed a commit to ksolana/sui that referenced this pull request Jun 14, 2024
* 032424-failure-loop: ini

* 032424-failure-loop: fix_bb_terminator

* 032424-failure-loop: fix in sbc::Bytecode::Label

* 032424-failure-loop: add failure-tests

* 032424-failure-loop: fix_bb_terminators and fix_terminator_in_prev_bb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants