From 07549c31f66b9bc729c174dbcde4e048895de637 Mon Sep 17 00:00:00 2001 From: Sebastian Krynski Date: Wed, 6 Mar 2024 12:50:11 +0000 Subject: [PATCH 1/4] try --- .../compiler/native/lower_function_llvm.cpp | 41 +++++++++++++++---- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/rir/src/compiler/native/lower_function_llvm.cpp b/rir/src/compiler/native/lower_function_llvm.cpp index 01e1cd912..092428270 100644 --- a/rir/src/compiler/native/lower_function_llvm.cpp +++ b/rir/src/compiler/native/lower_function_llvm.cpp @@ -2977,11 +2977,20 @@ void LowerFunctionLLVM::compile() { case blt("anyNA"): case blt("is.na"): if (irep == Rep::i32) { - setVal(i, - builder.CreateSelect( - builder.CreateICmpEQ(a, c(NA_INTEGER)), - constant(R_TrueValue, orep), - constant(R_FalseValue, orep))); + auto arg = i->arg(0).val(); + if (arg->type.maybeNAOrNaN()) { + // i->print(std::cerr, true); + // std::cerr << "arg: " << (uint) arg->tag << " + // " << arg->type << " \n"; + // assert(i->arg(0).val()->type.maybeNAOrNaN()); + setVal(i, builder.CreateSelect( + builder.CreateICmpEQ( + a, c(NA_INTEGER)), + constant(R_TrueValue, orep), + constant(R_FalseValue, orep))); + } else { + setVal(i, constant(R_FalseValue, orep)); + } } else if (irep == Rep::f64) { setVal(i, builder.CreateSelect( builder.CreateFCmpUNE(a, a), @@ -4731,13 +4740,27 @@ void LowerFunctionLLVM::compile() { assert(r1 == Rep::i32); res = load(arg); if (!arg->type.isA(RType::logical)) { - res = builder.CreateSelect( - builder.CreateICmpEQ(res, c(NA_INTEGER)), - c(NA_LOGICAL), + + // std::cerr << "arg: " << (uint) arg->tag << " " << + // arg->type << " \n"; + + if (arg->type.maybeNAOrNaN()) { + + res = builder.CreateSelect( + builder.CreateICmpEQ(res, c(NA_INTEGER)), + c(NA_LOGICAL), + builder.CreateSelect( + builder.CreateICmpEQ(res, c(0)), + constant(R_FalseValue, t::Int), + constant(R_TrueValue, t::Int))); + } else { + // std::cerr << " not NA \n"; + // assert(false); builder.CreateSelect( builder.CreateICmpEQ(res, c(0)), constant(R_FalseValue, t::Int), - constant(R_TrueValue, t::Int))); + constant(R_TrueValue, t::Int)); + } } } From 1245655813ee63133f30e43670a3c725fea4a775 Mon Sep 17 00:00:00 2001 From: Sebastian Krynski Date: Wed, 6 Mar 2024 16:42:25 +0000 Subject: [PATCH 2/4] more --- .../compiler/native/lower_function_llvm.cpp | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/rir/src/compiler/native/lower_function_llvm.cpp b/rir/src/compiler/native/lower_function_llvm.cpp index 092428270..1b3686047 100644 --- a/rir/src/compiler/native/lower_function_llvm.cpp +++ b/rir/src/compiler/native/lower_function_llvm.cpp @@ -2975,31 +2975,30 @@ void LowerFunctionLLVM::compile() { break; } case blt("anyNA"): - case blt("is.na"): - if (irep == Rep::i32) { - auto arg = i->arg(0).val(); - if (arg->type.maybeNAOrNaN()) { - // i->print(std::cerr, true); - // std::cerr << "arg: " << (uint) arg->tag << " - // " << arg->type << " \n"; - // assert(i->arg(0).val()->type.maybeNAOrNaN()); + case blt("is.na"): { + + auto arg = i->arg(0).val(); + if (!arg->type.maybeNAOrNaN()) { + setVal(i, constant(R_FalseValue, orep)); + } else { + + if (irep == Rep::i32) { setVal(i, builder.CreateSelect( builder.CreateICmpEQ( a, c(NA_INTEGER)), constant(R_TrueValue, orep), constant(R_FalseValue, orep))); + } else if (irep == Rep::f64) { + setVal(i, builder.CreateSelect( + builder.CreateFCmpUNE(a, a), + constant(R_TrueValue, orep), + constant(R_FalseValue, orep))); } else { - setVal(i, constant(R_FalseValue, orep)); + done = false; } - } else if (irep == Rep::f64) { - setVal(i, builder.CreateSelect( - builder.CreateFCmpUNE(a, a), - constant(R_TrueValue, orep), - constant(R_FalseValue, orep))); - } else { - done = false; } break; + } case blt("is.object"): if (irep == Rep::SEXP) { setVal(i, builder.CreateSelect( From 8d64453641c68535578e53da41533bcb835e6546 Mon Sep 17 00:00:00 2001 From: Sebastian Krynski Date: Thu, 7 Mar 2024 10:23:49 +0000 Subject: [PATCH 3/4] minor --- .../compiler/native/lower_function_llvm.cpp | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/rir/src/compiler/native/lower_function_llvm.cpp b/rir/src/compiler/native/lower_function_llvm.cpp index 1b3686047..dad714006 100644 --- a/rir/src/compiler/native/lower_function_llvm.cpp +++ b/rir/src/compiler/native/lower_function_llvm.cpp @@ -2978,25 +2978,25 @@ void LowerFunctionLLVM::compile() { case blt("is.na"): { auto arg = i->arg(0).val(); - if (!arg->type.maybeNAOrNaN()) { + + if ((irep == Rep::i32 || irep == Rep::f64) && + !arg->type.maybeNAOrNaN()) { setVal(i, constant(R_FalseValue, orep)); + } else if (irep == Rep::i32) { + setVal(i, + builder.CreateSelect( + builder.CreateICmpEQ(a, c(NA_INTEGER)), + constant(R_TrueValue, orep), + constant(R_FalseValue, orep))); + } else if (irep == Rep::f64) { + setVal(i, builder.CreateSelect( + builder.CreateFCmpUNE(a, a), + constant(R_TrueValue, orep), + constant(R_FalseValue, orep))); } else { - - if (irep == Rep::i32) { - setVal(i, builder.CreateSelect( - builder.CreateICmpEQ( - a, c(NA_INTEGER)), - constant(R_TrueValue, orep), - constant(R_FalseValue, orep))); - } else if (irep == Rep::f64) { - setVal(i, builder.CreateSelect( - builder.CreateFCmpUNE(a, a), - constant(R_TrueValue, orep), - constant(R_FalseValue, orep))); - } else { - done = false; - } + done = false; } + break; } case blt("is.object"): From 76de8a6f2b53974b501a39d5e5c6db2c5571bf7d Mon Sep 17 00:00:00 2001 From: Sebastian Krynski Date: Thu, 7 Mar 2024 16:10:28 +0000 Subject: [PATCH 4/4] minor fix --- .../compiler/native/lower_function_llvm.cpp | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/rir/src/compiler/native/lower_function_llvm.cpp b/rir/src/compiler/native/lower_function_llvm.cpp index dad714006..49de18837 100644 --- a/rir/src/compiler/native/lower_function_llvm.cpp +++ b/rir/src/compiler/native/lower_function_llvm.cpp @@ -4740,25 +4740,20 @@ void LowerFunctionLLVM::compile() { res = load(arg); if (!arg->type.isA(RType::logical)) { - // std::cerr << "arg: " << (uint) arg->tag << " " << - // arg->type << " \n"; + auto nonNAIntegerToBool = [&]() { + return builder.CreateSelect( + builder.CreateICmpEQ(res, c(0)), + constant(R_FalseValue, t::Int), + constant(R_TrueValue, t::Int)); + }; if (arg->type.maybeNAOrNaN()) { res = builder.CreateSelect( builder.CreateICmpEQ(res, c(NA_INTEGER)), - c(NA_LOGICAL), - builder.CreateSelect( - builder.CreateICmpEQ(res, c(0)), - constant(R_FalseValue, t::Int), - constant(R_TrueValue, t::Int))); + c(NA_LOGICAL), nonNAIntegerToBool()); } else { - // std::cerr << " not NA \n"; - // assert(false); - builder.CreateSelect( - builder.CreateICmpEQ(res, c(0)), - constant(R_FalseValue, t::Int), - constant(R_TrueValue, t::Int)); + res = nonNAIntegerToBool(); } } }