Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR] Vector constants #700

Merged
merged 3 commits into from
Jul 1, 2024
Merged

Conversation

dkolsen-pgi
Copy link
Collaborator

Implement vector constants in ClangIR.

Resolves issue #498 - Add a cir.const_vec, simlar to cir.const_array and cir.const_struct

Create a new kind of attribute, cir::ConstVectorAttr in the code or #cir.const_vector in the assembly, which represents a compile-time value of a cir::VectorType. The values for the elements within the vector are stored as attributes within an mlir::ArrayAttr.

When doing CodeGen for a prvalue of vector type, try to represent it as cir.const #cir.const_vector first. If that fails, most likely because some of the elements are not compile-time values, fall back to the existing code that uses a cir.vec.create operation.

When lowering directly to LLVM IR, lower cir.const #cir.const_vector as llvm.mlir.constant(dense<[...]> : _type_) : _type_.

When lowering through other MLIR dialects, lower
cir.const #cir.const_vector as arith.constant dense<[...]> : _type_.

No new tests were added, but the expected results of the existing tests that use vector constants were updated.

Implement vector constants in ClangIR.

Resolves issue llvm#498 - Add a `cir.const_vec`, simlar to `cir.const_array`
and `cir.const_struct`

Create a new kind of attribute, `cir::ConstVectorAttr` in the code or
`#cir.const_vector` in the assembly, which represents a compile-time
value of a `cir::VectorType`.  The values for the elements within the
vector are stored as attributes within an `mlir::ArrayAttr`.

When doing CodeGen for a prvalue of vector type, try to represent it as
`cir.const #cir.const_vector` first.  If that fails, most likely because
some of the elements are not compile-time values, fall back to the
existing code that uses a `cir.vec.create` operation.

When lowering directly to LLVM IR, lower `cir.const #cir.const_vector`
as `llvm.mlir.constant(dense<[...]> : _type_) : _type_`.

When lowering through other MLIR dialects, lower
`cir.const #cir.const_vector` as `arith.constant dense<[...]> : _type_`.

No new tests were added, but the expected results of the existing tests
that use vector constants were updated.
Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

LGTM after few nits.

Another nice future addition would be to add a VecCreateOp::fold method, and whenever all elts are const, fold it to a mlir::cir::ConstVectorAttr. Then you could add VecCreateOp to the list in MergeCleanupsPass::runOnOperation(), to get that going.

(Alternatively, with this approach we wouldn't even need to change CIRGen to directly emit the attribute, but emitting it directly also makes sense)

clang/lib/CIR/Dialect/IR/CIRDialect.cpp Outdated Show resolved Hide resolved
clang/include/clang/CIR/Dialect/IR/CIRAttrs.td Outdated Show resolved Hide resolved
clang/test/CIR/CodeGen/vectype-ext.cpp Show resolved Hide resolved
Change the type of the elements field of `cir::ConstVectorAttr` from
`mlir::Attribute` to `mlir::ArrayAttr`.  The verifier already checked
that it was an `ArrayAttr`.  This change moves that checking to a lower
level, into the CIR builder or parser.
@dkolsen-pgi
Copy link
Collaborator Author

I resolved the code review requests, except for VecCreateOp::fold. Would you like me to do that now, or separately?

@bcardosolopes
Copy link
Member

I resolved the code review requests, except for VecCreateOp::fold. Would you like me to do that now, or separately?

That can come next! Good to go after resolving conflicts.

The change from member functions isa/cast/dyn_cast to the corresponding
global functions had to be done manually in the code that was unique to
branch feature/vec-const.
@dkolsen-pgi
Copy link
Collaborator Author

I rebased my branch (with manual work to avoid the deprecated isa/cast/dyn_cast functions). It should now be free of conflicts.

@bcardosolopes bcardosolopes merged commit d999c53 into llvm:main Jul 1, 2024
6 checks 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.

None yet

2 participants