Skip to content

Generate a typedesc instruction once for record and tuple types #41945

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

Conversation

chiranSachintha
Copy link
Member

@chiranSachintha chiranSachintha commented Jan 8, 2024

Purpose

$title.

Fixes #38844
Fixes #41946

In this PR, I have generated a new 'typedesc' (type descriptor) only once for the tuple and record.
For example:

type Foo record {||};
public function main() {
    Foo f = {};
    record {|int a;|} r = {a: 1};
}

We have desugared the above program as follows:

type Foo record {||};
typedesc<Foo> $typedesc$Foo = Foo;
public function main() {
    Foo f = {};
    typedesc<record {| int a; |}> $typedesc$$anonType$_0 = record {| int a; |}
    record {|int a;|} r = {a: 1};
}

We then use the generated 'typedesc' when creating a map value using that type.

Check List

  • Read the Contributing Guide
  • Updated Change Log
  • Checked Tooling Support (#)
  • Added necessary tests
    • Unit Tests
    • Spec Conformance Tests
    • Integration Tests
    • Ballerina By Example Tests
  • Increased Test Coverage
  • Added necessary documentation
    • API documentation
    • Module documentation in Module.md files
    • Ballerina By Examples

@chiranSachintha chiranSachintha added the Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. label Jan 8, 2024
@@ -97,8 +89,8 @@ public Object instantiate(Strand s) {
@Override
public Object instantiate(Strand s, BInitialValueEntry[] initialValues) {
Type referredType = getImpliedType(this.describingType);
if (referredType.getTag() == TypeTags.MAP_TAG) {
return new MapValueImpl(this.describingType, (BMapInitialValueEntry[]) initialValues);
if (referredType.getTag() == TypeTags.MAP_TAG || referredType.getTag() == TypeTags.RECORD_TYPE_TAG) {
Copy link
Member

Choose a reason for hiding this comment

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

Shall we create a local variable for the result of referredType.getTag() (or use a switch on that), since we are using it 3 times in the if else block

Comment on lines +2434 to +2436
BIROperand symbolVarOperand = this.env.symbolVarMap.containsKey(typeSymbol.annotations) ?
new BIROperand(this.env.symbolVarMap.get(typeSymbol.annotations)) :
new BIROperand(this.globalVarMap.get(typeSymbol.annotations));
Copy link
Member

Choose a reason for hiding this comment

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

Assuming we don't put null to symbolVarMap shall we change this such that we are not doing the lookup on symbolVarMap twice

Suggested change
BIROperand symbolVarOperand = this.env.symbolVarMap.containsKey(typeSymbol.annotations) ?
new BIROperand(this.env.symbolVarMap.get(typeSymbol.annotations)) :
new BIROperand(this.globalVarMap.get(typeSymbol.annotations));
BVarSymbol typeAnnotations = typeSymbol.annotations;
BIRVariableDcl localDecl = this.env.symbolVarMap.get(typeAnnotations)
BIROperand symbolVarOperand = localDecl != null ?
new BIROperand(localDecl) :
new BIROperand(this.globalVarMap.get(typeAnnotations));

Copy link

This PR has been open for more than 15 days with no activity. This will be closed in 3 days unless the stale label is removed or commented.

Comment on lines +2386 to +2389
} else {
createNewTypedescInst(type, type, pos);
return new BIRNonTerminator.NewStructure(pos, toVarRef, this.env.targetOperand, fields);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit

Suggested change
} else {
createNewTypedescInst(type, type, pos);
return new BIRNonTerminator.NewStructure(pos, toVarRef, this.env.targetOperand, fields);
}
}
createNewTypedescInst(type, type, pos);
return new BIRNonTerminator.NewStructure(pos, toVarRef, this.env.targetOperand, fields);

}

for (Map.Entry<BSymbol, BIRGlobalVariableDcl> entry : this.globalVarMap.entrySet()) {
if (isTypeDescSymbol(entry.getKey(), Types.getImpliedType(type))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we separate Types.getImpliedType(type) into a variable so that function is called only once.

Comment on lines +2438 to +2440
} else {
return new BIRNonTerminator.NewTypeDesc(position, toVarRef, resolveType, closures);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
} else {
return new BIRNonTerminator.NewTypeDesc(position, toVarRef, resolveType, closures);
}
}
return new BIRNonTerminator.NewTypeDesc(position, toVarRef, resolveType, closures);

if (getTypedescVariable(listConstructorExprType) != null) {
return new BIRNonTerminator.NewArray(pos, listConstructorExprType, toVarRef,
new BIROperand(getTypedescVariable(listConstructorExprType)), sizeOp, initialValues);

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change

globalVar.getBType());
}

private boolean containsErrorType(BType type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need a separate function for this as this wraps only a single statement

Copy link

This PR has been open for more than 15 days with no activity. This will be closed in 3 days unless the stale label is removed or commented.

@github-actions github-actions bot added the Stale label Feb 22, 2024
Copy link

Closed PR due to inactivity for more than 18 days.

Copy link

github-actions bot commented Aug 2, 2024

This PR has been open for more than 15 days with no activity. This will be closed in 3 days unless the stale label is removed or commented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times.
Projects
None yet
3 participants