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][CirGen][Bugfix] Fixes __sync_fetch_and_add for unsigned integers #799

Merged
merged 4 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ makeBinaryAtomicValue(CIRGenFunction &cgf, mlir::cir::AtomicFetchKind kind,

Address destAddr = checkAtomicAlignment(cgf, expr);
auto &builder = cgf.getBuilder();
auto intType = builder.getSIntNTy(cgf.getContext().getTypeSize(typ));
mlir::cir::IntType intType;
if (expr->getArg(0)->getType()->getPointeeType()->isUnsignedIntegerType())
intType = builder.getUIntNTy(cgf.getContext().getTypeSize(typ));
else
intType = builder.getSIntNTy(cgf.getContext().getTypeSize(typ));
Copy link
Member

Choose a reason for hiding this comment

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

You can also do a oneliner here:

mlir::cir::IntType intType = expr->getArg(0)->getType()->getPointeeType()->isUnsignedIntegerType() ? builder.getUIntNTy(cgf.getContext().getTypeSize(typ)) : builder.getSIntNTy(cgf.getContext().getTypeSize(typ));

clang-format will help out

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

mlir::Value val = cgf.buildScalarExpr(expr->getArg(1));
mlir::Type valueType = val.getType();
val = buildToInt(cgf, val, typ, intType);
Expand Down
9 changes: 9 additions & 0 deletions clang/test/CIR/CodeGen/ssync-fetch-and-add.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - | FileCheck %s

#include <stdint.h>

void foo(int64_t x) {
__sync_fetch_and_add(&x, 1);
Copy link
Member

Choose a reason for hiding this comment

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

Instead of the new testfiles, incorporate this with other __sync_fetch_and_add testing in clang/test/CIR/CodeGen/atomic.cpp

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

}

// CHECK: %0 = cir.alloca !s64i, !cir.ptr<!s64i>, ["x", init] {alignment = 8 : i64}
9 changes: 9 additions & 0 deletions clang/test/CIR/CodeGen/usync-fetch-and-add.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - | FileCheck %s

#include <stdint.h>

void foo(uint64_t x) {
__sync_fetch_and_add(&x, 1);
}

// CHECK: %0 = cir.alloca !u64i, !cir.ptr<!u64i>, ["x", init] {alignment = 8 : i64}