From 6816d9af643141cbddd0dac6378204c8a4f22cd3 Mon Sep 17 00:00:00 2001 From: kchristin Date: Mon, 2 Sep 2024 23:52:19 +0300 Subject: [PATCH] Initialize pointers as nullptr and add a default int case for literal init --- lib/Differentiator/ConstantFolder.cpp | 18 +++++++++++++++--- lib/Differentiator/VisitorBase.cpp | 2 +- test/Gradient/Assignments.C | 2 +- test/Gradient/Gradients.C | 4 ++-- test/Gradient/Loops.C | 18 +++++++++--------- test/Gradient/Pointers.C | 4 ++-- test/Gradient/STLCustomDerivatives.C | 10 +++++----- 7 files changed, 35 insertions(+), 23 deletions(-) diff --git a/lib/Differentiator/ConstantFolder.cpp b/lib/Differentiator/ConstantFolder.cpp index e8094f872..23fc4ea98 100644 --- a/lib/Differentiator/ConstantFolder.cpp +++ b/lib/Differentiator/ConstantFolder.cpp @@ -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)) { @@ -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, @@ -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 diff --git a/lib/Differentiator/VisitorBase.cpp b/lib/Differentiator/VisitorBase.cpp index 318bcb172..55f975f79 100644 --- a/lib/Differentiator/VisitorBase.cpp +++ b/lib/Differentiator/VisitorBase.cpp @@ -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(); diff --git a/test/Gradient/Assignments.C b/test/Gradient/Assignments.C index f8c4fef72..5716c0b02 100644 --- a/test/Gradient/Assignments.C +++ b/test/Gradient/Assignments.C @@ -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 *_d_ref = {}; +//CHECK-NEXT: clad::array *_d_ref = nullptr; //CHECK-NEXT: clad::array *ref = {}; //CHECK-NEXT: double _t0; //CHECK-NEXT: clad::array _d_list = {{2U|2UL}}; diff --git a/test/Gradient/Gradients.C b/test/Gradient/Gradients.C index 1a2329673..891aa2ecf 100644 --- a/test/Gradient/Gradients.C +++ b/test/Gradient/Gradients.C @@ -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; diff --git a/test/Gradient/Loops.C b/test/Gradient/Loops.C index d08b2ad9d..0c7bd00b3 100644 --- a/test/Gradient/Loops.C +++ b/test/Gradient/Loops.C @@ -1605,7 +1605,7 @@ double fn19(double* arr, int n) { // CHECK-NEXT: int i = 0; // CHECK-NEXT: clad::tape _t1 = {}; // CHECK-NEXT: clad::tape _t2 = {}; -// CHECK-NEXT: double *_d_ref = {}; +// CHECK-NEXT: double *_d_ref = nullptr; // CHECK-NEXT: double *ref = {}; // CHECK-NEXT: clad::tape _t3 = {}; // CHECK-NEXT: double _d_res = 0.; @@ -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; @@ -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; @@ -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; @@ -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 _t1 = {}; //CHECK-NEXT: double _d_res = 0.; //CHECK-NEXT: double res = 0; diff --git a/test/Gradient/Pointers.C b/test/Gradient/Pointers.C index ccafb0f97..e4884e0df 100644 --- a/test/Gradient/Pointers.C +++ b/test/Gradient/Pointers.C @@ -152,8 +152,8 @@ double pointerParam(const double* arr, size_t n) { // CHECK-NEXT: size_t i = 0UL; // CHECK-NEXT: clad::tape _t1 = {}; // CHECK-NEXT: clad::tape _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 _t4 = {}; // CHECK-NEXT: clad::tape _t5 = {}; // CHECK-NEXT: clad::tape _t6 = {}; diff --git a/test/Gradient/STLCustomDerivatives.C b/test/Gradient/STLCustomDerivatives.C index bd2f9f58d..cf6fef18d 100644 --- a/test/Gradient/STLCustomDerivatives.C +++ b/test/Gradient/STLCustomDerivatives.C @@ -181,22 +181,22 @@ int main() { // CHECK: void fn12_grad(double u, double v, double *_d_u, double *_d_v) { // CHECK-NEXT: std::vector _t1; -// CHECK-NEXT: double *_d_ref0 = {}; +// CHECK-NEXT: double *_d_ref0 = nullptr; // CHECK-NEXT: double *ref0 = {}; // CHECK-NEXT: std::vector _t3; -// CHECK-NEXT: double *_d_ref1 = {}; +// CHECK-NEXT: double *_d_ref1 = nullptr; // CHECK-NEXT: double *ref1 = {}; // CHECK-NEXT: std::vector _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 _t19; -// CHECK-NEXT: double *_d_ref00 = {}; +// CHECK-NEXT: double *_d_ref00 = nullptr; // CHECK-NEXT: double *ref00 = {}; // CHECK-NEXT: std::vector _t21; -// CHECK-NEXT: double *_d_ref10 = {}; +// CHECK-NEXT: double *_d_ref10 = nullptr; // CHECK-NEXT: double *ref10 = {}; // CHECK-NEXT: double _t23; // CHECK-NEXT: double _t24;