This is an archive of the discontinued LLVM Phabricator instance.

[mlir][llvm] Introduce some constant folding.
ClosedPublic

Authored by Moxinilian on Jun 16 2023, 6:30 AM.

Details

Summary

This revision introduces some constant folding features to the LLVM
dialect. This specific choice of operations to cover is intended to
allow the elimination of logic generated by mem2reg with memset in the
common case of memsets of constant values.

This also introduces new verifiers for integer extension operations.
This lead to a fix in SPIRV to LLVM conversion, as it would sometimes
generate invalid ZExt and SExt operations.

Diff Detail

Event Timeline

Moxinilian created this revision.Jun 16 2023, 6:30 AM
Herald added a reviewer: dcaballe. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
Moxinilian requested review of this revision.Jun 16 2023, 6:30 AM
gysit added inline comments.Jun 16 2023, 7:25 AM
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2718

We may have to verify if the target size is smaller than the bitwidth of arg?

3285

Do we need some checks here to verify the value can actually be converted into a constant?

Similar to:

arith::ConstantOp::isBuildableWith

The arith dialect uses the method in arith::ConstantOp::materialize.

Moxinilian marked 2 inline comments as done.
Moxinilian edited the summary of this revision. (Show Details)

Address comments + apply bugfix in SPIRVtoLLVM.

Moxinilian added inline comments.Jun 19 2023, 4:22 AM
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
3285

ConstantOp has a verifier of its own that should prevent it. In any case, I don't expect folding of operations returning LLVM dialect types to generate incompatible types except in case of bugs.

Moxinilian added inline comments.Jun 19 2023, 4:24 AM
mlir/lib/Conversion/SPIRVToLLVM/SPIRVToLLVM.cpp
1228

I am not sure if this would require a bitcast or not, as I do not know if the type may differ. None of the tests failed after using the same value, which makes me think the types are always consistent, but I am not sure. @kuhar would you maybe know?

Moxinilian edited the summary of this revision. (Show Details)Jun 19 2023, 4:46 AM
gysit added a comment.Jun 19 2023, 7:42 AM

Question would it make sense to check only the scalar case in the newly added verifier? Supporting all cases may trigger quite a bit of additional work and we are only folding scalar ops if I understand correctly?

mlir/lib/Conversion/SPIRVToLLVM/SPIRVToLLVM.cpp
54

nit: It actually returns the bitwidth?

1230

nit: please also add braces around this return.

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
2539

nit: should we call it verifyExtOp or verifyIntegerExt etc. since we do also handle zero extension not just sign extension.

2541

I think we have to support LLVMVectorType as well?

The following function isCompatibleVectorType is used by LLVM dialect to check if the vector type is supported.

2544

nit: instead of append it may be nicer to use the steaming operator:

op.emitError() << "input type is a vector but output type is an integer";

or even better but the error message directly intro parenthesis

op.emitError("input type is a vector but output type is an integer");
3285

I think the idea is to return nullptr if a type cannot be converted. In that case the folding would fail but the compilation would still be fine. Catching this in the verifier would be too late.

Moxinilian marked 5 inline comments as done.

Address comments.

Mark comments as done.

Moxinilian marked an inline comment as done.

Address forgotten comment.

Address forgotten comment.

gysit added inline comments.Jun 20 2023, 11:39 PM
mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
35

nit: can you check if all the new headers are necessary?

2535

I know LLVM dialect is not consistent in that regard but can you introduce two section headers

//===----------------------------------------------------------------------===//
// LLVM::ZExtOp
//===----------------------------------------------------------------------===//

//===----------------------------------------------------------------------===//
// LLVM::SExtOp
//===----------------------------------------------------------------------===//

And put both the folders and verifiers below that headers? I think it makes sense that they are in the same place.

I would also move the shl and or operation folders to the other definitions that exist for these ops if there are any.

2539

nit: can you add a doc comment.

mlir/test/Dialect/LLVMIR/invalid.mlir
1471

nit: I think a test for "input type is a vector but output type is an integer" is missing?

Moxinilian marked 4 inline comments as done.

Address comments.

Address comments.

gysit accepted this revision.Jun 21 2023, 5:43 AM

Thanks LGTM!

This revision is now accepted and ready to land.Jun 21 2023, 5:43 AM
This revision was automatically updated to reflect the committed changes.