Skip to content

feat(dataflow): dead code analysis#659

Open
axelcool1234 wants to merge 6 commits into
opencompl:mainfrom
axelcool1234:dead-code
Open

feat(dataflow): dead code analysis#659
axelcool1234 wants to merge 6 commits into
opencompl:mainfrom
axelcool1234:dead-code

Conversation

@axelcool1234

@axelcool1234 axelcool1234 commented May 28, 2026

Copy link
Copy Markdown
Collaborator

Implements a dataflow analysis for unreachable code.

Note - MLIR has a much more advanced understanding of region semantics I didn't know how to implement with VeIR's current capabilities. I have left a large number of TODOs in places where they should be handled.

@axelcool1234 axelcool1234 force-pushed the dead-code branch 3 times, most recently from d0d5c42 to a0c7ca0 Compare May 29, 2026 01:30
@axelcool1234 axelcool1234 marked this pull request as draft May 29, 2026 01:51
@axelcool1234 axelcool1234 force-pushed the dead-code branch 8 times, most recently from 2d0f49c to 426f54f Compare June 4, 2026 01:47
@axelcool1234 axelcool1234 force-pushed the dead-code branch 13 times, most recently from 75fee26 to 2a358ff Compare June 12, 2026 06:57
@axelcool1234 axelcool1234 changed the title Dead Code Analysis feat(dataflow): dead code analysis Jun 12, 2026
@axelcool1234 axelcool1234 force-pushed the dead-code branch 4 times, most recently from b291656 to 44a9a71 Compare June 12, 2026 21:29
@axelcool1234 axelcool1234 marked this pull request as ready for review June 14, 2026 00:08
@axelcool1234 axelcool1234 requested review from math-fehr and regehr June 14, 2026 00:08

@axelcool1234 axelcool1234 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Review for myself

Comment on lines +245 to +246
if point.prev! irCtx = none then
return dfCtx

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Go back to MLIR and change this to a panic if needed.

Comment on lines +279 to +292
-- Recurse on nested operations.
for regionPtr in (op.get! irCtx).regions do
-- TODO: If we haven't seen a symbol table yet, check if the current
-- operation has one. If so, update the flag to allow for resolving
-- callables in nested regions.
let region := regionPtr.get! irCtx
let mut maybeBlock := region.firstBlock
while let some block := maybeBlock do
let mut maybeOp := (block.get! irCtx).firstOp
while let some nestedOp := maybeOp do
dfCtx := initializeRecursively nestedOp dfCtx irCtx
maybeOp := (nestedOp.get! irCtx).next
maybeBlock := (block.get! irCtx).next
dfCtx

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is a common pattern that I have done many many times, and I know @regehr has done for CSE. Perhaps this should be a helper in Basic.lean? @math-fehr

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 does seem like a useful helper. we don't want to return a list-- that's slow. rather is should be some sort of visitor / iterator / callback.

| return "analysis did not converge"
return renderReport (check top dfCtx parserState)

/-- Dead code helpers. -/

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.

@axelcool1234 if this code is potentially useful for multiple analyses, then this file seems like a good place to put it. but if it is only helping dead code analysis, then just put it there and don't create this extra file

def isBlockLive (dfCtx : DataFlowContext) (block : BlockPtr) (irCtx : IRContext OpCode) : Bool :=
match dfCtx.getFact? .executable (.InsertPoint (InsertPoint.atStart! block irCtx)) with
| some fact => fact.live
| none => false

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.

what are the circumstances in which there would not be a fact available here? can these circumstances be avoided? if not, should you signal a real error here instead of merely returning false?

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