Skip to content

Add lowering support for static_assert#326

Open
devalgupta404 wants to merge 2 commits intop4lang:mainfrom
devalgupta404:issue-183
Open

Add lowering support for static_assert#326
devalgupta404 wants to merge 2 commits intop4lang:mainfrom
devalgupta404:issue-183

Conversation

@devalgupta404
Copy link
Copy Markdown
Contributor

issue #183
Add lowering support for static_assert in the P4HIRToCoreLib conversion.
First I evaluates the assertion condition at compile time. If the condition is a constant boolean true, the call is replaced with p4hir.const #true. If the condition evaluates to false, a diagnostic (static assertion failed) is emitted during lowering.

Covers Four Test cases

  • static_assert(true)
  • static_assert(true, "message")
  • static_assert(false)
  • static_assert(false, "message")

I am using '--split-input-file' to run each cases in an independent module otherwise repeated definations of 'static_assert' will cause duplicate symbol errors.


// Ensure the condition is a compile-time constant
Attribute constAttr;
if (!matchPattern(args[0], m_Constant(&constAttr)))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This should be done differently. Per spec:

these functions require compile-time known values as arguments, thus they can be used to enforce compile-time invariants

So, the condition could really be resolved later and error out here is a premature.

We'd need to low:

  • Lower the generic extern call into a (new) op with clear semantics
  • Provide evaluation step (we can just constfold out things with positive assertions, but errors must be done in a separate pass to keep context)

Signed-off-by: devalgupta404 <devalgupta4@gmail.com>
Signed-off-by: devalgupta404 <devalgupta4@gmail.com>
@devalgupta404
Copy link
Copy Markdown
Contributor Author

Hi @asl,
I’ve updated the PR to follow the suggested approach.
i Introduced a a new p4corelib.static_assert op with clear semantics, Updated lowering to avoid enforcing compile-time constants during conversion also Implemented a dedicated EvaluateStaticAssertPass and Added folding support in the op for early canonicalization of true cases as well.

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.

2 participants