Skip to content

Commit

Permalink
[CIR][Lowering] fix lowering for the structs inited with zeros (llvm#315
Browse files Browse the repository at this point in the history
)

Basically that is, the  next code should work now
```
typedef struct {
    int a;
    int b;
} A;
...
A a = {0, 0};
```
  • Loading branch information
gitoleg authored and lanza committed Jan 29, 2024
1 parent 9eb6612 commit 611bc4c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// This file implements lowering of CIR operations to LLVMIR.
//
//===----------------------------------------------------------------------===//

#include "mlir/Conversion/AffineToStandard/AffineToStandard.h"
#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
Expand Down Expand Up @@ -1076,6 +1075,17 @@ class CIRConstantLowering
rewriter.replaceAllUsesWith(op, initVal);
rewriter.eraseOp(op);
return mlir::success();
} else if (auto strTy = op.getType().dyn_cast<mlir::cir::StructType>()) {
if (auto zero = op.getValue().dyn_cast<mlir::cir::ZeroAttr>()) {
auto initVal =
lowerCirAttrAsValue(op, zero, rewriter, typeConverter);
rewriter.replaceAllUsesWith(op, initVal);
rewriter.eraseOp(op);
return mlir::success();
}

return op.emitError()
<< "unsupported lowering for struct constant type " << op.getType();
} else
return op.emitError() << "unsupported constant type " << op.getType();

Expand Down
12 changes: 12 additions & 0 deletions clang/test/CIR/Lowering/struct-init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-llvm %s -o %t.ll
// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
struct S {
int x;
};

// LLVM: define void @zeroInit
// LLVM: [[TMP0:%.*]] = alloca %struct.S, i64 1
// LLVM: store %struct.S zeroinitializer, ptr [[TMP0]]
void zeroInit() {
struct S s = {0};
}

0 comments on commit 611bc4c

Please sign in to comment.