llvm / llvm/llvm-project

[CIR] Upstream missing support for new and delete expressions

Open
#192,328 3 comments 0 reactions 0 assignees View on GitHub
ClangIR
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

### Overview

Upstream code from the incubator project to replace errorNYI calls in the
CIR codegen implementation related to `new` and `delete` expression handling:
- **Constant array new initialization**: handling multi-dimensional constant
arrays in `emitNewArrayInitializer` by converting the type, creating a
`withElementType` pointer, and multiplying init-list elements by the constant
array element count
- **Placement new with delete pairing**: adding allocator arguments (size +
allocation pointer) when a reserved global placement new has a non-reserved
`operator delete`
- **Null check for new**: creating a `cir::IfOp` with null comparison and
`ConditionalEvaluation` to guard initialization when the allocation might
return null

Upstream NYI locations:
- `CIRGenExprCXX.cpp` — `emitNewArrayInitializer`: constant array init with
multi-dimensional arrays
- `CIRGenExprCXX.cpp` — `emitCXXNewExpr`: reserved placement new with delete
- `CIRGenExprCXX.cpp` — `emitCXXNewExpr`: null check for allocations

### Suggested minimal test case

```cpp
#include

struct S { int x; ~S() {} };

// Conditional cleanup
S *test_conditional_new(bool cond) {
return cond ? new S{42} : nullptr;
}

// Constant array new init
int *test_array_new() {
return new int[3][4]();
}

// Null check
int *test_new_nothrow() {
return new (std::nothrow) int(42);
}
```

### Existing incubator tests

- `clang/test/CIR/CodeGen/new.cpp`
- `clang/test/CIR/CodeGen/new-null.cpp`
- `clang/test/CIR/CodeGen/array-new-init.cpp`

Contributor guide

Open the contributing guide

Research direction

Start in CIRGenExprCXX.cpp at the three listed NYI locations: emitNewArrayInitializer and the two emitCXXNewExpr cases. Run clang/test/CIR/CodeGen/new.cpp, new-null.cpp, and array-new-init.cpp with the suggested examples, then verify the new, delete-pairing, multidimensional initialization, and null-check cases are covered without the upstream NYI paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.