Skip to content

Commit

Permalink
Cleanup (#1205)
Browse files Browse the repository at this point in the history
* update gnur

* Cleanup

Remove out-of-sync structs and use gnur's
Use Rf_ prefix for all R internals we call
Try to deal with the #define eval Rf_eval idiom

* some includes and formatting

* delete unused files

* small refactoring

* try sanitizer

* missing include

* fix perf regression
  • Loading branch information
JanJecmen committed May 14, 2022
1 parent 4002968 commit ac2fe85
Show file tree
Hide file tree
Showing 45 changed files with 334 additions and 596 deletions.
3 changes: 2 additions & 1 deletion .vscode/c_cpp_properties.json
Expand Up @@ -9,7 +9,8 @@
"${workspaceFolder}/external/llvm-12.0.0.src/include/**"
],
"defines": [
"ENABLE_SLOWASSERT"
"ENABLE_SLOWASSERT",
"R_NO_REMAP"
],
"cStandard": "c11",
"cppStandard": "c++14",
Expand Down
2 changes: 1 addition & 1 deletion external/custom-r
129 changes: 12 additions & 117 deletions rir/src/R/Funtab.h
Expand Up @@ -2,136 +2,31 @@
#define RIR_FUNTAB_H

#include "BuiltinIds.h"

#include "R/r.h"

#include <cassert>

typedef struct sxpinfo_struct_rjit {
unsigned int type : 5; /* ==> (FUNSXP == 99) %% 2^5 == 3 == CLOSXP
* -> warning: `type' is narrower than values
* of its type
* when SEXPTYPE was an enum */
unsigned int obj : 1;
unsigned int named : 2;
unsigned int gp : 16;
unsigned int mark : 1;
unsigned int debug : 1;
unsigned int trace : 1; /* functions and memory tracing */
unsigned int spare : 1; /* currently unused */
unsigned int gcgen : 1; /* old generation number */
unsigned int gccls : 3; /* node class */
} sxpifo_struct_rjit; /* Tot: 32 */

typedef struct cons_rjit {
SEXP car;
SEXP cdr;
SEXP tag;
} cons_rjit;

typedef struct sexprec_rjit {
struct sxpinfo_struct_rjit sxpinfo;
SEXP attrib;
SEXP gengc_next_node, gengc_prev_node;
union {
struct cons_rjit cons;
int i;
} u;
} sexprec_rjit;

typedef SEXP (*CCODE)(SEXP, SEXP, SEXP, SEXP);

/* Information for Deparsing Expressions */
typedef enum {
PP_INVALID = 0,
PP_ASSIGN = 1,
PP_ASSIGN2 = 2,
PP_BINARY = 3,
PP_BINARY2 = 4,
PP_BREAK = 5,
PP_CURLY = 6,
PP_FOR = 7,
PP_FUNCALL = 8,
PP_FUNCTION = 9,
PP_IF = 10,
PP_NEXT = 11,
PP_PAREN = 12,
PP_RETURN = 13,
PP_SUBASS = 14,
PP_SUBSET = 15,
PP_WHILE = 16,
PP_UNARY = 17,
PP_DOLLAR = 18,
PP_FOREIGN = 19,
PP_REPEAT = 20
} PPkind;

typedef enum {
PREC_FN = 0,
PREC_LEFT = 1,
PREC_EQ = 2,
PREC_RIGHT = 3,
PREC_TILDE = 4,
PREC_OR = 5,
PREC_AND = 6,
PREC_NOT = 7,
PREC_COMPARE = 8,
PREC_SUM = 9,
PREC_PROD = 10,
PREC_PERCENT = 11,
PREC_COLON = 12,
PREC_SIGN = 13,
PREC_POWER = 14,
PREC_DOLLAR = 15,
PREC_NS = 16,
PREC_SUBSET = 17
} PPprec;
static inline int getBuiltinNr(SEXP f) { return f->u.primsxp.offset; }

typedef struct {
PPkind kind; /* deparse kind */
PPprec precedence; /* operator precedence */
unsigned int rightassoc; /* right associative? */
} PPinfo;

/* The type definitions for the table of built-in functions. */
/* This table can be found in ../main/names.c */
typedef struct {
char* name; /* print name */
CCODE cfun; /* c-code address */
int code; /* offset within c-code */
int eval; /* evaluate args? */
int arity; /* function arity */
PPinfo gram; /* pretty-print info */
} FUNTAB;

extern FUNTAB R_FunTab[];

/** Returns us the CCODE object from R_FunTab based on name.
TODO This exists in gnu-r (names.c), when integrated inside, we want to make
use of it.
*/
static inline CCODE getBuiltin(SEXP f) {
int i = ((sexprec_rjit*)f)->u.i;
return R_FunTab[i].cfun;
return R_FunTab[getBuiltinNr(f)].cfun;
}
static inline int getBuiltinNr(SEXP f) { return ((sexprec_rjit*)f)->u.i; }

static inline const char* getBuiltinName(int i) { return R_FunTab[i].name; }
static inline int getBuiltinArity(SEXP f) { return R_FunTab[getBuiltinNr(f)].arity; }
static inline int getFlag(SEXP f) {
int i = ((sexprec_rjit*)f)->u.i;
return (((R_FunTab[i].eval) / 100) % 10);
static inline const char* getBuiltinName(SEXP f) {
return getBuiltinName(getBuiltinNr(f));
}
static inline int getFlag(int i) { return (((R_FunTab[i].eval) / 100) % 10); }

static inline bool builtinUpdatesVisibility(int id) { return getFlag(id) < 2; }
static inline bool builtinVisibility(int id) {
assert(builtinUpdatesVisibility(id));
return getFlag(id) != 1;
static inline int getBuiltinArity(SEXP f) {
return R_FunTab[getBuiltinNr(f)].arity;
}

static inline int getFlag(int i) { return ((R_FunTab[i].eval) / 100) % 10; }
static inline int getFlag(SEXP f) { return getFlag(getBuiltinNr(f)); }

static inline SEXP getBuiltinFun(char const* name) {
assert(R_FunTab[rir::blt(name)].eval % 10 == 1 && "Only use for BUILTINSXP");
assert(R_FunTab[rir::blt(name)].eval % 10 == 1 &&
"Only use for BUILTINSXP");
if (R_FunTab[rir::blt(name)].eval % 100 / 10 == 0)
return Rf_install(name)->u.symsxp.value;
else
Expand Down
56 changes: 0 additions & 56 deletions rir/src/R/RVector.cpp

This file was deleted.

57 changes: 0 additions & 57 deletions rir/src/R/RVector.h

This file was deleted.

50 changes: 26 additions & 24 deletions rir/src/R/r.h
Expand Up @@ -3,39 +3,42 @@

#include "common.h"

#define R_NO_REMAP
#define USE_RINTERNALS
#include <R.h>
#include <R_ext/Print.h>
#include <Rinterface.h>
#define USE_RINTERNALS
#include <Rinternals.h>

// r print statement
#include <R_ext/Print.h>

#undef error
#undef TRUE
#undef FALSE
#undef length
#undef eval
#undef cons

// Use the function versions (some of the names clash with LLVM)
#undef isNull
inline bool isNull(SEXP s) { return TYPEOF(s) == NILSXP; }
#undef isSymbol
#undef isLogical
#undef isReal
#undef isComplex
inline bool isComplex(SEXP s) { return TYPEOF(s) == CPLXSXP; }
#undef isExpression
#undef isEnvironment
#undef isString
inline bool isString(SEXP s) { return TYPEOF(s) == STRSXP; }

#undef isObject
inline bool isObject(SEXP s) { return OBJECT(s) != 0; }

#undef isVector

inline bool Rf_isNull(SEXP s) { return TYPEOF(s) == NILSXP; }
inline bool Rf_isSymbol(SEXP s) { return TYPEOF(s) == SYMSXP; }
inline bool Rf_isLogical(SEXP s) { return TYPEOF(s) == LGLSXP; }
inline bool Rf_isReal(SEXP s) { return TYPEOF(s) == REALSXP; }
inline bool Rf_isComplex(SEXP s) { return TYPEOF(s) == CPLXSXP; }
inline bool Rf_isExpression(SEXP s) { return TYPEOF(s) == EXPRSXP; }
inline bool Rf_isEnvironment(SEXP s) { return TYPEOF(s) == ENVSXP; }
inline bool Rf_isString(SEXP s) { return TYPEOF(s) == STRSXP; }
inline bool Rf_isObject(SEXP s) { return OBJECT(s) != 0; }

// Clash with LLVM
#undef PI

// Get rid of the macro version of this
#undef R_CheckStack

// Bypass PREXPR from GNU R which causes code objects to be converted to AST
#undef PREXPR
inline SEXP PREXPR(SEXP pr) {
// bypassing PREXPR from Gnur, which causes code objects to be converted to
// AST
SLOWASSERT(TYPEOF(pr) == PROMSXP);
auto res = pr->u.promsxp.expr;
if (TYPEOF(res) == BCODESXP)
Expand All @@ -44,10 +47,11 @@ inline SEXP PREXPR(SEXP pr) {
}

extern "C" {
extern FUNTAB R_FunTab[];
extern SEXP R_TrueValue;
extern SEXP R_FalseValue;
extern SEXP R_LogicalNAValue;
};
}

// Performance critical stuff copied from Rinlinedfun.h

Expand Down Expand Up @@ -119,5 +123,3 @@ extern int R_PPStackTop;
} while (0)

#endif

#undef R_CheckStack
1 change: 0 additions & 1 deletion rir/src/api.cpp
Expand Up @@ -3,7 +3,6 @@
*/

#include "api.h"
#include "R/Funtab.h"
#include "R/Serialize.h"
#include "bc/BC.h"
#include "bc/Compiler.h"
Expand Down
1 change: 1 addition & 0 deletions rir/src/api.h
Expand Up @@ -4,6 +4,7 @@
#include "R/r.h"
#include "compiler/log/debug.h"
#include "runtime/Context.h"

#include <stdint.h>

#define REXPORT extern "C"
Expand Down

0 comments on commit ac2fe85

Please sign in to comment.