Skip to content

Added advice for students - #100

Open
novo52 wants to merge 5 commits into
masterfrom
backend-advice
Open

Added advice for students#100
novo52 wants to merge 5 commits into
masterfrom
backend-advice

Conversation

@novo52

@novo52 novo52 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

I want the students to understand certain decisions that will affect them down the line when implementing their compilers.

I am a bit iffy on how it is presented. I appreciate criticism.

@kuzi117

kuzi117 commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Two cents from years gone by: I found it difficult for many students to intuit the pure SSA model with phi instructions (not sure how this exactly maps in mlir) when we gave them all of the code on a slide. It only gets more complicated when you start implementing arbitrary control flow and you might as well start implementing a data flow pass just to make sure you've hit all of the possible sources for a value. Anecdotally, I tried to do it in my instructor solution and it was extremely tedious and not worth it.

Students have enough challenges in the course as it is, and I think you've only added more since I ran it. I'd present this less like a choice and more like "you should use the alloca model, but strive to understand what would happen if you wanted to do it the other way." Frankly, if clang doesn't bother at -O0, don't bang your head on the wall trying to outdo it.

Comment thread gazprea/impl/backend.rst Outdated
placing the phi / block-argument merges yourself.

.. Warning::
The SSA model is the more difficult of the two to implement, but can allow for more

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.

Do you know for a fact that more optimizations can occur? I might be persuaded myself if you have a demonstrable example. My guess is that LLVM has a pass that does this for you, so that it can catch all the opportunities.

@rcunrau

rcunrau commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

I think this stuff is great - thank you! I'm wondering if instead of a footnote in the spec, perhaps I could do a lecture on memory-less vs memory dialects in MLIR. I would have to work with you so that I can understand the nuances myself, unless you want to do the lecture yourself.

@novo52

novo52 commented Jul 17, 2026

Copy link
Copy Markdown
Contributor Author

I made the changes suggested. I wonder if I should just remove the SSA section entirely?

@Sir-NoChill Sir-NoChill left a comment

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.

Looking good, but I think we should be deliberate about suggesting that the students use the tools that a dialect comes with by default to get some of the productivity, optimization and stylistic gains that MLIR can provide. I left some comments, I'd like to discuss them.

Comment thread gazprea/impl/backend.rst
Comment on lines +15 to +16
you load from and store to, or as an SSA value that is defined once. **Use the
memory model.** It keeps code generation local and mechanical. The choice is the

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.

I'm not sure we should outright suggest this. There is a good argument for using value-typed systems that FLang uses in FIR, namely they associate an fir expression that can grow and shrink (above memref) and then bufferize down to fir.box and fir.ref types with allocations. There is also precedent for this in the sparse_tensor dialect, which has a 'push_back' op that creates a new value type whenever adding an element to an array.

I agree that if the students were to implement this all themselves it just results in a ton of extra work, but they can steal from other dialects to make it somewhat easier. Especially with the bots, adopting the FIR/CLangIR methods would be effective in my mind.

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.

Could also point to the pytorch dialect that has the NonValueTensorType which might be a useful model. It exists above bufferization but is also mutable, I think that's the model we should follow in the solution.

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 think for this year, we should hold off on these types of improvements so the feedback from the change in evaluation is less noisy.

Comment thread gazprea/impl/backend.rst
Comment on lines +71 to +72
*Scalars.* Map each variable name to its *current* SSA value: a read is a lookup, an
assignment produces a new value and rebinds the name. There are no slots and no

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.

Might be useful to link to the bufferization documentation, since this comes with a little bit more nuance when you look at the bufferization passes: https://mlir.llvm.org/docs/Bufferization/#destination-passing-style

We may also want to discuss the composite types here too, and how MLIR can handle the destructuring for the students automatically. Probably pointing to toy ch7

Comment thread gazprea/impl/backend.rst Outdated
Comment on lines +102 to +104
%0 = llvm.mlir.constant(128 : i64) : i64
%1 = llvm.call @malloc(%0) : (i64) -> !llvm.ptr
llvm.call @free(%1) : (!llvm.ptr) -> ()

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.

I don't think the students should prefer to use the llvm dialect for the most part. In mlir this is supposed to be treated as a target dialect, rather than a dialect in which you write your IR, otherwise you just end up writing LLVM IR since MLIR cannot optimize LLVM IR (see the output dialects). I think we should prefer using func, memref and arith for this.

Base automatically changed from gazprea-2026-changes to master July 30, 2026 03:57
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.

5 participants