-
Notifications
You must be signed in to change notification settings - Fork 47
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
Verify tmp registers #1010
base: main
Are you sure you want to change the base?
Verify tmp registers #1010
Conversation
compiler/modules/bir/verify.bal
Outdated
@@ -10,12 +10,15 @@ class VerifyContext { | |||
private final Module mod; | |||
private final t:Context tc; | |||
private final FunctionDefn defn; | |||
final FunctionCode code; | |||
boolean[] tmpRegisterUsed = []; |
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.
tmpRegisterUsed looks like it's final too.
I suggest you make both of them private, and then add methods to VerifyContext to encapsulate how you use them.
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.
can we make tmpRegisterUsed
final since we modify the boolean values?
compiler/modules/bir/verify.bal
Outdated
|DecimalArithmeticBinaryInsn|CompareInsn|ListConstructInsn|ListGetInsn|MappingConstructInsn | ||
|MappingGetInsn|StringConcatInsn|EqualityInsn; | ||
|
||
type ResultInsn IntArithmeticBinaryInsn|IntNoPanicArithmeticBinaryInsn|IntBitwiseBinaryInsn |
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.
Might be simpler to do something like readonly & record { *ResultInsnBase; readonly ... }
(if jBallerina accepts it)
compiler/modules/bir/verify.bal
Outdated
@@ -10,6 +10,7 @@ class VerifyContext { | |||
private final Module mod; | |||
private final t:Context tc; | |||
private final FunctionDefn defn; | |||
private final boolean[] tmpRegisterUsed = []; |
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.
I would just pass it as a param and not make VC stateful.
compiler/modules/bir/verify.bal
Outdated
Register narrowed = r.underlying; | ||
while narrowed is NarrowRegister { | ||
narrowed = narrowed.underlying; | ||
} | ||
if !t:isSubtype(vc.typeContext(), r.semType, narrowed.semType) { | ||
return vc.invalidErr("narrow register is not a subtype of narrowed register", <Position>r.pos); | ||
} |
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.
I don't think we need to check the every time we read form a NarrowRegister, just checking when we first narrow (ie: TypeMerge/TypeBr) is enough. That overlaps with my work.
compiler/modules/bir/verify.bal
Outdated
type ResultInsn readonly & record { *ResultInsnBase; }; | ||
|
||
function verifyInsnRegisterKinds(VerifyContext vc, Insn insn) returns Error? { | ||
if insn is ResultInsn { |
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.
Can we use if insn is ResultInsnBase {
instead?
compiler/modules/bir/verify.bal
Outdated
|
||
function verifyInsnRegisterKinds(VerifyContext vc, Insn insn) returns Error? { | ||
if insn is ResultInsn { | ||
if (vc.getNumTmpRegisters() > insn.result.number) && vc.getUsedTmpRegisters(insn.result.number) { |
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.
if tmpRegisterUsed[insn.result.number] {
Pre-populate the tmpRegisterUsed array with false
upto registers.length()
. Then you don't have to check the length.
compiler/modules/bir/verify.bal
Outdated
if r is FinalRegister { | ||
return vc.invalidErr("invalid register kind final for insn: " + r.kind, <Position>r.pos); | ||
} |
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.
Can we do this check at verifyListSet/verifyMappingSet?
compiler/modules/bir/verify.bal
Outdated
if insn is BranchInsn|CatchInsn { | ||
return; | ||
} | ||
else if insn is CallInsn { | ||
foreach Operand arg in insn.args { | ||
check verifyRegisterKind(vc, arg, tmpRegisterUsed, insn.pos); | ||
} | ||
} |
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.
Move this to match and get rid of the if
compiler/modules/bir/verify.bal
Outdated
var { operand } => { | ||
check verifyRegisterKind(vc, operand, tmpRegisterUsed, insn.pos); | ||
} | ||
var { operands } => { |
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.
Can we do var { operands } | { args: operands } => {
not sure if it works in jBallerina, if not I think a issue needs to be created.
Anyway I think CallInsn
needs to be changed to have operands
instead of args
for consistency. Please also create an issue for that.
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.
We can do var { operands } | var { args: operands } => {
compiler/modules/bir/verify.bal
Outdated
if tmpRegisterUsed[insn.result.number] { | ||
return vc.invalidErr("tmp register defined in multiple places", insn.pos); |
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.
I think we should call this tmpRegisterInit
. defined
doesn't sound right. Can we say multiple assignments to a tmp register
?
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.
Fixed.
compiler/modules/bir/verify.bal
Outdated
} | ||
} | ||
|
||
function verifyRegisterKind(VerifyContext vc, Operand r, boolean[] tmpRegisterInit, Position pos) returns Error? { |
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.
Current name doesn't explain what it does. How about verifyTmpOprandInitialized
compiler/modules/bir/verify.bal
Outdated
} | ||
} | ||
|
||
function verifyTmpOprandInitialized(VerifyContext vc, Operand r, boolean[] tmpRegisterInit, Position pos) returns Error? { |
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.
verifyTmpOperandInitialized
compiler/modules/bir/verify.bal
Outdated
@@ -67,18 +67,59 @@ class VerifyContext { | |||
|
|||
public function verifyFunctionCode(Module mod, FunctionDefn defn, FunctionCode code) returns Error? { | |||
VerifyContext cx = new(mod, defn); | |||
boolean[] tmpRegisterInit = []; |
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.
On a second though, let's call this tmpRegisterInitialized
to match with the func name.
compiler/modules/bir/verify.bal
Outdated
} | ||
} | ||
|
||
function verifyInsnRegisterKinds(VerifyContext vc, Insn insn, boolean[] tmpRegisterInit) returns Error? { |
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.
verifyTmpRegisters
compiler/modules/bir/verify.bal
Outdated
if insn.operands[0] is FinalRegister { | ||
return vc.invalidErr("invalid register kind final for ListSetInsn", insn.pos); | ||
} |
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.
This is wrong.
import ballerina/io;
public function main() {
final int[] i = [1, 2, 3];
i[0] = 4;
io:println(i);
}
Above is a valid Ballerina program.
compiler/modules/bir/verify.bal
Outdated
if insn.operands[0] is FinalRegister { | ||
return vc.invalidErr("invalid register kind final for MappingSetInsn", insn.pos); | ||
} |
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.
Same as above
compiler/modules/front/stmt.bal
Outdated
if matched is bir:TmpRegister { | ||
bir:AssignTmpRegister result = cx.createAssignTmpRegister(matched.semType, matched.pos); | ||
bir:AssignInsn insn = { result, operand: matched, pos : stmt.startPos }; | ||
matched = result; |
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.
In a match we are only reading matched
, aren't we ? why do we need AssignTmpRegister
?
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.
In a case like
match x + 1 {
2 => {
return "1";
}
4 => {
return "2";
}
}
even though matched is a TmpRegister
it will be used in multiple blocks for Equality instructions, so to avoid that I think we can either do something like this or change all expr code gens to create AssignTmpRegister
instead of Tmp depending on the situation.
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.
I think we'll have to change the definition of TmpRegister
to, assigned once, but can be used form multiple blocks.
@jclark what do you think?
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.
I believe this is already the case, the problem is when the TmpRegister
is used in a different block than the block it was initially assigned.
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.
but can be used multiple times
I meant
but can be used from multiple block
let me edit.
@jclark this is ready for you to review. |
compiler/modules/bir/verify.bal
Outdated
} | ||
} | ||
|
||
function verifyTmpRegisters(VerifyContext vc, Insn insn, boolean[] tmpRegisterInitialized) returns Error? { |
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.
let's call this verifyTmpRegisterAssignment
@@ -67,18 +67,34 @@ class VerifyContext { | |||
|
|||
public function verifyFunctionCode(Module mod, FunctionDefn defn, FunctionCode code) returns Error? { | |||
VerifyContext cx = new(mod, defn); | |||
boolean[] tmpRegisterInitialized = []; | |||
if code.registers.length() > 0 { |
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.
Cleaner to do tmpRegisterIntialized.setLength(code.registers.length());
|
||
function verifyTmpRegisterAssignment(VerifyContext vc, Insn insn, boolean[] tmpRegisterInitialized) returns Error? { | ||
if insn is ResultInsnBase { | ||
if tmpRegisterInitialized[insn.result.number] { |
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.
Add int num = insn.result.number;
Purpose
Goals
Approach
User stories
Release note
Documentation
Training
Certification
Marketing
Automation tests
Security checks
Samples
Related PRs
Migrations (if applicable)
Test environment
Learning