Skip to content

Commit

Permalink
Initialize pointers as nullptr and add a default int case for literal…
Browse files Browse the repository at this point in the history
… init
  • Loading branch information
kchristin22 committed Sep 2, 2024
1 parent df2617d commit 6816d9a
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 23 deletions.
18 changes: 15 additions & 3 deletions lib/Differentiator/ConstantFolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ namespace clad {
return new (C) CXXBoolLiteralExpr(val, QT, noLoc);
}

static Expr* synthesizeLiteral(QualType QT, ASTContext& C) {
assert(QT->isPointerType() && "Not a pointer type.");
SourceLocation noLoc;
return new (C) CXXNullPtrLiteralExpr(QT, noLoc);
}

Expr* ConstantFolder::trivialFold(Expr* E) {
Expr::EvalResult Result;
if (E->EvaluateAsRValue(Result, m_Context)) {
Expand Down Expand Up @@ -132,9 +138,12 @@ namespace clad {

Expr* ConstantFolder::synthesizeLiteral(QualType QT, ASTContext& C,
uint64_t val) {
//SourceLocation noLoc;
// SourceLocation noLoc;
Expr* Result = 0;
if (QT->isBooleanType()) {
QT = QT.getCanonicalType();
if (QT->isPointerType()) {
Result = clad::synthesizeLiteral(QT, C);
} else if (QT->isBooleanType()) {
Result = clad::synthesizeLiteral(QT, C, (bool)val);
} else if (QT->isIntegralType(C)) {
llvm::APInt APVal(C.getIntWidth(QT), val,
Expand All @@ -143,8 +152,11 @@ namespace clad {
} else if (QT->isRealFloatingType()) {
llvm::APFloat APVal(C.getFloatTypeSemantics(QT), val);
Result = clad::synthesizeLiteral(QT, C, APVal);
} else {
// FIXME: Handle other types, like Complex, Structs, typedefs, etc.
Result = ConstantFolder::synthesizeLiteral(C.IntTy, C, val);
}
assert(Result && "Must not be zero.");
assert(Result && "Unsupported type for constant folding.");
return Result;
}
} // end namespace clad
2 changes: 1 addition & 1 deletion lib/Differentiator/VisitorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ namespace clad {
// FIXME: Consolidate other uses of synthesizeLiteral for creation 0 or 1.
if (T->isVoidType())
return nullptr;
if (T->isScalarType() && !T->isPointerType()) {
if ((T->isScalarType() || T->isPointerType()) && !T->isReferenceType()) {
ExprResult Zero =
ConstantFolder::synthesizeLiteral(T, m_Context, /*val=*/0);
return Zero.get();
Expand Down
2 changes: 1 addition & 1 deletion test/Gradient/Assignments.C
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ double f23(double x, double y) {

//CHECK: void f23_grad(double x, double y, double *_d_x, double *_d_y) {
//CHECK-NEXT: bool _cond0;
//CHECK-NEXT: clad::array<double> *_d_ref = {};
//CHECK-NEXT: clad::array<double> *_d_ref = nullptr;
//CHECK-NEXT: clad::array<double> *ref = {};
//CHECK-NEXT: double _t0;
//CHECK-NEXT: clad::array<double> _d_list = {{2U|2UL}};
Expand Down
4 changes: 2 additions & 2 deletions test/Gradient/Gradients.C
Original file line number Diff line number Diff line change
Expand Up @@ -1121,9 +1121,9 @@ double f_ref_in_rhs(double x, double y) {

//CHECK: void f_ref_in_rhs_grad(double x, double y, double *_d_x, double *_d_y) {
//CHECK-NEXT: bool _cond0;
//CHECK-NEXT: double *_d_ref_x = {};
//CHECK-NEXT: double *_d_ref_x = nullptr;
//CHECK-NEXT: double *ref_x = {};
//CHECK-NEXT: double *_d_ref_y = {};
//CHECK-NEXT: double *_d_ref_y = nullptr;
//CHECK-NEXT: double *ref_y = {};
//CHECK-NEXT: {
//CHECK-NEXT: _cond0 = x != 55;
Expand Down
18 changes: 9 additions & 9 deletions test/Gradient/Loops.C
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ double fn19(double* arr, int n) {
// CHECK-NEXT: int i = 0;
// CHECK-NEXT: clad::tape<double *> _t1 = {};
// CHECK-NEXT: clad::tape<double *> _t2 = {};
// CHECK-NEXT: double *_d_ref = {};
// CHECK-NEXT: double *_d_ref = nullptr;
// CHECK-NEXT: double *ref = {};
// CHECK-NEXT: clad::tape<double> _t3 = {};
// CHECK-NEXT: double _d_res = 0.;
Expand Down Expand Up @@ -2708,8 +2708,8 @@ double fn34(double x, double y){
//CHECK-NEXT: double *__begin10 = __range10;
//CHECK-NEXT: double *_d___begin1 = _d___range1;
//CHECK-NEXT: double *__end10 = __range10 + {{3|3L}};
//CHECK-NEXT: double *_d_i = {};
//CHECK-NEXT: double *i = {};
//CHECK-NEXT: double *_d_i = nullptr;
//CHECK-NEXT: double *i = nullptr;
//CHECK-NEXT: for (; __begin10 != __end10; ++__begin10 , ++_d___begin1) {
//CHECK-NEXT: {
//CHECK-NEXT: _d_i = &*_d___begin1;
Expand Down Expand Up @@ -2782,8 +2782,8 @@ double fn35(double x, double y){
//CHECK-NEXT: double *__begin10 = __range10;
//CHECK-NEXT: double *_d___begin1 = _d___range1;
//CHECK-NEXT: double *__end10 = __range10 + {{3|3L}};
//CHECK-NEXT: double *_d_i = {};
//CHECK-NEXT: double *i = {};
//CHECK-NEXT: double *_d_i = nullptr;
//CHECK-NEXT: double *i = nullptr;
//CHECK-NEXT: for (; __begin10 != __end10; ++__begin10 , ++_d___begin1) {
//CHECK-NEXT: {
//CHECK-NEXT: _d_i = &*_d___begin1;
Expand All @@ -2798,8 +2798,8 @@ double fn35(double x, double y){
//CHECK-NEXT: double *__begin20 = __range20;
//CHECK-NEXT: double *_d___begin2 = _d___range2;
//CHECK-NEXT: double *__end20 = __range20 + {{3|3L}};
//CHECK-NEXT: double *_d_j = {};
//CHECK-NEXT: double *j = {};
//CHECK-NEXT: double *_d_j = nullptr;
//CHECK-NEXT: double *j = nullptr;
//CHECK-NEXT: for (; __begin20 != __end20; ++__begin20 , ++_d___begin2) {
//CHECK-NEXT: {
//CHECK-NEXT: _d_j = &*_d___begin2;
Expand Down Expand Up @@ -3097,8 +3097,8 @@ double fn39(double x) {
}

//CHECK: void fn39_grad(double x, double *_d_x) {
//CHECK-NEXT: int *_d_i = {};
//CHECK-NEXT: {{const int *\*|const_iterator }}i = {};
//CHECK-NEXT: int *_d_i = nullptr;
//CHECK-NEXT: {{const int *\*|const_iterator }}i = nullptr;
//CHECK-NEXT: clad::tape<double> _t1 = {};
//CHECK-NEXT: double _d_res = 0.;
//CHECK-NEXT: double res = 0;
Expand Down
4 changes: 2 additions & 2 deletions test/Gradient/Pointers.C
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ double pointerParam(const double* arr, size_t n) {
// CHECK-NEXT: size_t i = 0UL;
// CHECK-NEXT: clad::tape<size_t *> _t1 = {};
// CHECK-NEXT: clad::tape<size_t *> _t3 = {};
// CHECK-NEXT: size_t *_d_j = {};
// CHECK-NEXT: size_t *j = {};
// CHECK-NEXT: size_t *_d_j = nullptr;
// CHECK-NEXT: size_t *j = nullptr;
// CHECK-NEXT: clad::tape<double> _t4 = {};
// CHECK-NEXT: clad::tape<const double *> _t5 = {};
// CHECK-NEXT: clad::tape<double *> _t6 = {};
Expand Down
10 changes: 5 additions & 5 deletions test/Gradient/STLCustomDerivatives.C
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,22 @@ int main() {

// CHECK: void fn12_grad(double u, double v, double *_d_u, double *_d_v) {
// CHECK-NEXT: std::vector<double> _t1;
// CHECK-NEXT: double *_d_ref0 = {};
// CHECK-NEXT: double *_d_ref0 = nullptr;
// CHECK-NEXT: double *ref0 = {};
// CHECK-NEXT: std::vector<double> _t3;
// CHECK-NEXT: double *_d_ref1 = {};
// CHECK-NEXT: double *_d_ref1 = nullptr;
// CHECK-NEXT: double *ref1 = {};
// CHECK-NEXT: std::vector<double> _t5;
// CHECK-NEXT: double *_d_ref2 = {};
// CHECK-NEXT: double *_d_ref2 = nullptr;
// CHECK-NEXT: double *ref2 = {};
// CHECK-NEXT: double _t7;
// CHECK-NEXT: double _t8;
// CHECK-NEXT: double _t9;
// CHECK-NEXT: std::vector<double> _t19;
// CHECK-NEXT: double *_d_ref00 = {};
// CHECK-NEXT: double *_d_ref00 = nullptr;
// CHECK-NEXT: double *ref00 = {};
// CHECK-NEXT: std::vector<double> _t21;
// CHECK-NEXT: double *_d_ref10 = {};
// CHECK-NEXT: double *_d_ref10 = nullptr;
// CHECK-NEXT: double *ref10 = {};
// CHECK-NEXT: double _t23;
// CHECK-NEXT: double _t24;
Expand Down

0 comments on commit 6816d9a

Please sign in to comment.