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][Dialect][NFC] Add some helpers to LoadOp #1021

Merged
merged 1,997 commits into from
Nov 5, 2024

Conversation

lanza
Copy link
Member

@lanza lanza commented Oct 29, 2024

These are just missing getters/setters that should be there already.
They are in use in a patch coming up. I'm splitting them out here for
reviewability.

Lancern and others added 30 commits October 12, 2024 00:29
This PR adds CIRGen and LLVMIR codegen for those not-yet-covered complex
casts, including explicit type cast expressions of complex types and
complex value promotion.

All type conversion expressions involving complex types should now
codegen.
This PR verifies `cir.get_global` has its result type correctly
annotated with address space of the referenced symbol. The documentation
is also updated to clarify this constraint.

`GlobalOp` is the main consideration. It's worth noting that if the
`cir.get_global` op references a function, we also (implicitly) checks
that its result pointer type has no address space attribute.
This PR sets proper address space when creating `cir.global` and
`cir.get_global`.

Different languages use different ways to encode the address space in
AST constructs (i.e. VarDecl *). OpenCL and SYCL use an address space
qualifier on the type of `VarDecl`, while CUDA uses separate AST
attributes like `CUDASharedAttr`. Similarily, some targets may want to
use special address space for global variables. So a per-language +
per-target hook is needed to provide this customization point. In the
LLVM CodeGen, it's the helper method `getGlobalVarAddressSpace` that
takes on the role.

For OpenCL C + SPIR-V combination, OpenCL C converts the address space
qualifier to corresponding LangAS, but SPIR-V does not require any
action.

This PR implements `global` qualifier in OpenCL C, but does not include
`constant` qualifier. Although the modified part works for `constant`,
CIRGen is not yet able to set constant attribute for global ops (there
is a TODO line).

Static variable decl and `local` qualifier work in a similar way and
come in later patches.
Unary decrement expression on floating point operands was lowered to
`fsub -1.0` by a typo. This PR fixes this bug.
…otected (#776)

This PR add a new CIR attribute `mlir::cir::VisibilityAttr` to represent
CIR visibility. It will represent C/C++ visibility type `Default`,
`Hidden`, `Protected`.

The PR handles the parsing, printing of CIR visibility and also lower to
LLVM.

After this PR, there will be more PR's to migrate CIRGen properties that
are currently querying MLIR visibility(e.g. `sym_visibility`), to
instead query CIR visibility, and remove MLIR's visibility from printing
and parsing.
This PR adds CIRGen and LLVMIR lowering for unary increment and
decrement expressions of complex types.

Currently blocked by #789 .
…d variables (#792)

In OpenCL, `local`-qualified variables are implicitly considered as
static. In order to support it, this PR unblocks code paths related to
OpenCL static declarations in `CIRGenDecl.cpp`.

Following the approach of LLVM CodeGen, a new class
`CIRGenOpenCLRuntime` is added to handle the language hook of creating
`local`-qualified variables. The default behavior of this hook is quite
simple. It forwards the call to `CGF.buildStaticVarDecl`.

So in CIR, the OpenCL local memory representation is equivalent to the
one defined by SPIR-LLVM convention: a `cir.global` with
`addrspace(local)` and *without initializer*, which corresponds to LLVM
`undef` initializer. See check lines in test for more details.

A `static global`-qualified variable is also added in the test to
exercise the static code path itself.
This patch fixes a bunch of pending review comments in #784:

 - Remove data layout attribute from address space testing
 - Remove incoherent comment
 - Rename abi_or_pref to abiOrPref
 - Make comments impersonal
 - Implement feature guard for ARM's CMSE secure call feature
 - Track volatile return times feature in CC lowering
 - Track missing features in the Itanium record builder
 - Remove incoherent fix me
 - Clarify comment regarding CIR record layout getter
 - Track missing cache for record layout getter
 - Remove unnecessary todo's
This PR adds the initial CIRGen support for pointer-to-member-functions.
It contains the following new types, attributes, and operations:

  - `!cir.method`, which represents the pointer-to-member-function type.
- `#cir.method`, which represents a literal pointer-to-member-function
value that points to ~~non-virtual~~ member functions.
- ~~`#cir.virtual_method`, which represents a literal
pointer-to-member-function value that points to virtual member
functions.~~
- ~~`cir.get_method_callee`~~ `cir.get_method`, which resolves a
pointer-to-member-function to a function pointer as the callee.

See the new test at `clang/test/CIR/CIRGen/pointer-to-member-func.cpp`
for how these new CIR stuff works to support
pointer-to-member-functions.
This PR adds a new `cir.select` operation. This operation won't be
generated directly by CIRGen but it is useful during further CIR to CIR
transformations.

This PR addresses #785 .
There is an implementation for inline variables processing at CIR. The
LIT test was taken from clang's cxx1z-inline-variables.cpp where the
same functionality is tested for Clang Code generation. The test can be
run as follows
```
bin/llvm-lit -v ../clang/test/CIR/CodeGen/cxx1z-inline-variables.cpp
```

Note: the pull request also contains a formatting change for two files:
- `clang/lib/CIR/Dialect/IR/CIRDataLayout.cpp`
- `clang/lib/CIR/Dialect/Transforms/TargetLowering/Targets/X86.cpp`
mixed signed and unsigned integer operator cause difference result when
index of array is negative
…800)

This PR refactors the LoweringPrepare pass and replaces various ternary
ops generated by LoweringPrepare with semantically equivalent select
ops.
#799)

`__sync_fetch_and_add` currently doesn't support unsigned integers. 

The following code snippet, for example, raises an error:  
```
#include <stdint.h>

void foo(uint64_t x) {
  __sync_fetch_and_add(&x, 1);
}
```
The error can be traced down to this line `auto intType =
builder.getSIntNTy(cgf.getContext().getTypeSize(typ));` from
`clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp`.
…ry fp2fp operations (#806)

This PR is the continuation and refinement of PR #434 which is
originally authored by @philnik777 . Does not update it in-place since I
don't have commit access to Nikolas' repo.

This PR basically just rebases #434 onto the latest `main`. I also
updated some naming used in the original PR to keep naming styles
consistent.

Co-authored-by: Nikolas Klauser <nikolasklauser@berlin.de>
Still missing CFG flattening and lowering, coming next.
ghehg and others added 4 commits October 30, 2024 11:27
We had some incorrect logic when creating functions and getting their
address which resulted in spurious "definition with the same mangled
name" errors. Fix that logic to match original CodeGen, which also fixes
these errors.

It's expected that decls can appear in the deferred decl list multiple
times, and CodeGen has to guard against that. In the case that triggered
the error, both `CIRGenerator::HandleInlineFunctionDefinition` and
CIRGenModule were deferring the declaration.

Something else I discovered here is that we emit these functions in the
opposite order as regular CodeGen: https://godbolt.org/z/4PrKG7h9b.
That might be a meaningful difference worth investigating further.

Fixes #991
…intrinsics (#1020)

In this PR, also changed `buildNeonShiftVector` to allow it generates
negative shift values. When the shift value is negative, the shift
amount vector is not used in any ShiftOp of IR (as they don't need sign
to know shift direction), instead, it is just input argument to shift
intrinsic function call.
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.

We already have enough assert checks for syncScopeID in clang/lib/CIR/CodeGen/CIRGenAtomic.cpp, please remove setAtomic and use setMemOrder directly. Feel free to add more asserts in CIRGen if you need, but no need to touch the operation this time around.

@lanza
Copy link
Member Author

lanza commented Oct 30, 2024

We already have enough assert checks for syncScopeID in clang/lib/CIR/CodeGen/CIRGenAtomic.cpp, please remove setAtomic and use setMemOrder directly. Feel free to add more asserts in CIRGen if you need, but no need to touch the operation this time around.

That's not right. Mapping setAtomic to setMemOrder is incorrect in cases where a syncScopeID should be there. All callers of setAtomic that should have a syncScopeID should crash as soon as we support syncScopeID to ensure they adjust their arguments. Threading use cases to setMemOrder hides this.

Mapping

SyncScopeID ssid = getSyncScope();
AtomicOrdering ao = getAtomicOrdering();
m.setAtomic(ao, ssid);

to

// TODO(CIR): syncscopeid
AtomicOrdering ao = getAtomicOrdering();
m.setMemOrder(ao);

won't be fixed until somebody is diligent about fixing all our TODOs. Assertion failures require fixing and will catch it immediately.

)

This PR fixes the notorious double whitespaces introduced by visibility
attribute, for `cir.func` only.

It uses "leading whitespace" for every print. And the printing of
visibility attr is properly guarded by a check of `!isDefault()`.

Double whitespaces in test files are removed.
@bcardosolopes
Copy link
Member

Well, the way this PR handles it doesn't help much either: setMemOrder can still be used and we have to catch that at review time, which is unreliable in the long run (e.g. reviewers without full context).

If that's the route you want to take, then please also do the following extra steps:

  • Change all calls to setMemOrder to setAtomic.
  • Add the same logic for StoreOp.
  • Add a comment to setAtomic saying folks should prefer to use that then setMemOrder directly.

PikachuHyA and others added 7 commits October 30, 2024 12:41
This patch introduces support for the abs family of built-in functions
(abs, labs, llabs).
Add lowering prepare logic to lower stores to cir.copy.

This bring LLVM lowering closer to OG and turns out the rest of the compiler
understands memcpys better and generate better assembly code for at least arm64
and x86_64.

Note that current lowering to memcpy is only using i32 intrinsic version,
this PR does not touch that code and that will be addressed in another PR.
Clang recognizes the function anyway, but this is an obvious error.
In addition, this PR enables ZeroAttr of vector type so that CIR can
generate a vector initialized with all zero values.
due to the issue described in
#1018,
the MLIR lowering for `memmove` has been excluded in this patch.
…bits (#1027)

This PR adds a support for the return values of struct types > 128 bits
in size.
As usually, lot's of copy-pasted lines from the original codegen, except
the `AllocaOp` replacement for the return value.
@@ -49,6 +49,7 @@ using namespace mlir;
#include "clang/CIR/Dialect/IR/CIROpsDialect.cpp.inc"
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
#include "clang/CIR/Interfaces/CIROpInterfaces.h"
#include <clang/CIR/MissingFeatures.h>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Curious this different inclusion style.
Anyway, I am compassionate about this blast effect 21f9795

Copy link
Member

Choose a reason for hiding this comment

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

Yea, this looks off, unless everything changes to angle based, this should be quotes based in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

What exactly do you mean? I'm including MissingFeatuers here because it's the right thing to do to ensure we're guarding against the SSID. But I agree that it's really ugly. Hopefully we see some change from #1025!

Copy link
Collaborator

Choose a reason for hiding this comment

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

We mean just a minor

Suggested change
#include <clang/CIR/MissingFeatures.h>
#include "clang/CIR/MissingFeatures.h"

Copy link
Member

Choose a reason for hiding this comment

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

Good to go after you fix this one!

gitoleg and others added 6 commits October 31, 2024 14:21
… convention lowering pass (#1034)

This PR adds a support for calls by  function pointers.

@sitio-couto   I think would be great if you'll also take a look
This PR adds calling convention lowering support for the int128 type on
x86_64.

This is a follow up on #953 .
Created using spr 1.3.5
@lanza lanza requested a review from bcardosolopes November 1, 2024 18:41
lanza added a commit that referenced this pull request Nov 1, 2024
These are just missing getters/setters that should be there already.
They are in use in a patch coming up. I'm splitting them out here for
reviewability.

Pull Request: #1021
Created using spr 1.3.5
@lanza lanza merged commit 9dfe49d into main Nov 5, 2024
53 of 55 checks passed
@lanza lanza deleted the spr/lanza/cirdialect-add-some-helpers-to-loadop branch November 5, 2024 18:48
lanza added a commit that referenced this pull request Mar 18, 2025
These are just missing getters/setters that should be there already.
They are in use in a patch coming up. I'm splitting them out here for
reviewability.

Reviewers: bcardosolopes

Pull Request: #1021
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