Skip to content

Instrumentation into Block IR#355

Merged
LPTK merged 299 commits intohkust-taco:hkmc2from
ChingLongTin:instrument
Mar 10, 2026
Merged

Instrumentation into Block IR#355
LPTK merged 299 commits intohkust-taco:hkmc2from
ChingLongTin:instrument

Conversation

@ChingLongTin
Copy link
Copy Markdown
Contributor

Call implementation will be after the algorithms for instrumenting functions are completed.

  1. allocating optionNme
    This changed some diff tests (OpenWildcard.mls, Imports.mls, ImportMLs.mls) that imported Option themselves.
    If I allocate optionNme after checking if stageCode is enabled, it fails to link to the file.
    Is there some way to avoid changing the diff tests, or is this fine?

  2. implementing I-Inst
    The current shape for Class C(n:s) includes the field names (n), but the field names are not available in Instantiation.
    The field names are used in sel. If the class is staged using I-Cls, then we can use the staged ClsLikeDefn to retrieve the symbol names, but when do we do when it isn't staged?

class A(a)
staged module B
  class C(a)
  fun f() = 
    new C(1) // ok, C is staged
    new A(1) // not ok, we don't know field names of A after instrumentation

@LPTK
Copy link
Copy Markdown
Contributor

LPTK commented Mar 4, 2026

Somehow there is a compilation error...

@LPTK LPTK requested a review from NeilKleistGao March 4, 2026 17:13
x = 1
x
fun g(x)(y, z)() = z
//│ ╔══[WARNING] Multiple parameter lists are not supported in shape propagation yet.
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.

Functions with multiple parameter lists can be desugared by the first-class function transformer (Enabled by :ftc. Probably after #404 is merged).

sym match
case clsSym: ClassSymbol =>
val name = scope.allocateOrGetName(sym)
transformParamsOpt(clsSym.defn.get.paramsOpt): paramsOpt =>
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.

Some class symbols (e.g., generated by another pass) may have no defn, which will lead to a crash.

Copy link
Copy Markdown
Contributor Author

@ChingLongTin ChingLongTin Mar 6, 2026

Choose a reason for hiding this comment

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

Since the parameters are necessary to reconstruct the class instance during specialization, this case throws a compiler error for now.

Copy link
Copy Markdown
Member

@NeilKleistGao NeilKleistGao Mar 6, 2026

Choose a reason for hiding this comment

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

I see. Then it is enough to raise a compilation error here. I will set a dummy definition with parameters in #404 so that our code can cooperate.

Is there any other information that you need to retrieve from the defn field?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

From our discussions last time, we need paramsOpt and auxParams.

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.

OK. It seems that dummy definitions do not help either. I think a proper way is to have another pass before starting instrumentation to collect these locally generated classes (represented in block IR). If there is no definition for the class symbol, you try to retrieve it from the collected data. These generated classes are not visible outside the module, so when we access a class via the path, it should always be possible to find the defined defn field.

Copy link
Copy Markdown
Contributor

@LPTK LPTK Mar 10, 2026

Choose a reason for hiding this comment

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

@ChingLongTin are you going to follow @NeilKleistGao's suggestion in this PR? Or in a follow-up one?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will do it in a follow-up one.

@ChingLongTin
Copy link
Copy Markdown
Contributor Author

There's a diff test change on backlog/NamedArgs.mls, codegen/BasicTerms.mls, codegen/Do.mls, ucs/syntax/NestedOpSplits.mls, from the printing of the Predef symbol. For example in the first file we have the following diff:

diff --git a/hkmc2/shared/src/test/mlscript/backlog/NamedArgs.mls b/hkmc2/shared/src/test/mlscript/backlog/NamedArgs.mls
index e5e0724..4c99118 100644
--- a/hkmc2/shared/src/test/mlscript/backlog/NamedArgs.mls
+++ b/hkmc2/shared/src/test/mlscript/backlog/NamedArgs.mls
@@ -37,7 +37,7 @@ id(a: 0)
 //│   stats = Nil
 //│   res = App:
 //│     lhs = SynthSel{sym=member:id⁰}:
-//│       prefix = Ref{sym=member:Predef⁰} of member:Predef⁰
+//│       prefix = Ref{sym=member:Predef¹} of member:Predef¹
 //│       nme = Ident of "id"
 //│     rhs = Tup of Ls of 
 //│       Fld:

@LPTK
Copy link
Copy Markdown
Contributor

LPTK commented Mar 9, 2026

@ChingLongTin, indeed, there was some flakiness that should be fixed now.

Copy link
Copy Markdown
Contributor

@LPTK LPTK left a comment

Choose a reason for hiding this comment

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

Ok, LGTM, but please remember to address the two remaining comments in follow-up PRs.

@LPTK LPTK merged commit 368dac6 into hkust-taco:hkmc2 Mar 10, 2026
1 check passed
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.

4 participants