12#ifndef OPENVDB_AX_CODEGEN_UTILS_HAS_BEEN_INCLUDED
13#define OPENVDB_AX_CODEGEN_UTILS_HAS_BEEN_INCLUDED
17#include "../ast/Tokens.h"
18#include "../Exceptions.h"
20#include <openvdb/version.h>
22#include <llvm/IR/IRBuilder.h>
23#include <llvm/IR/LLVMContext.h>
31#include <llvm/Support/raw_ostream.h>
44 (llvm::IRBuilder<>&, llvm::Value*, llvm::Type*)>;
47 (llvm::IRBuilder<>&, llvm::Value*, llvm::Value*)>;
56 std::vector<llvm::Type*>& types)
58 types.reserve(values.size());
59 for (
const auto& v : values) {
60 types.emplace_back(v->getType());
72 llvm::raw_string_ostream os(str);
86 llvm::Type* elementType = type;
87 while (elementType->isPointerTy()) {
88 elementType = elementType->getContainedType(0);
102template <
typename ValueT>
105 llvm::IRBuilder<>& builder)
107 llvm::Value* address =
108 llvm::ConstantInt::get(llvm::Type::getIntNTy(builder.getContext(),
sizeof(uintptr_t)*8),
109 reinterpret_cast<uintptr_t
>(ptr));
125 llvm::Value* size =
nullptr)
129 llvm::Function* parent = B.GetInsertBlock()->getParent();
130 assert(parent && !parent->empty());
131 auto IP = B.saveIP();
132 llvm::BasicBlock& block = parent->front();
133 if (block.empty()) B.SetInsertPoint(&block);
134 else B.SetInsertPoint(&(block.front()));
135 llvm::Value* result = B.CreateAlloca(type, size);
142 if (type == strtype) {
143 llvm::Value* cptr = B.CreateStructGEP(strtype, result, 0);
144 llvm::Value* sso = B.CreateStructGEP(strtype, result, 1);
145 llvm::Value* sso_load = B.CreateConstGEP2_64(sso, 0 ,0);
146 llvm::Value* len = B.CreateStructGEP(strtype, result, 2);
147 B.CreateStore(sso_load, cptr);
148 B.CreateStore(B.getInt64(0), len);
154inline llvm::Argument*
157 if (!F)
return nullptr;
158 if (idx >= F->arg_size())
return nullptr;
159 return llvm::cast<llvm::Argument>(F->arg_begin() + idx);
162inline llvm::Argument*
165 if (!F)
return nullptr;
166 for (
auto iter = F->arg_begin(); iter != F->arg_end(); ++iter) {
167 llvm::Argument* arg = llvm::cast<llvm::Argument>(iter);
168 if (arg->getName() == name)
return arg;
180 llvm::Type*
const typeB)
182 assert(typeA && (typeA->isIntegerTy() || typeA->isFloatingPointTy()) &&
183 "First Type in typePrecedence is not a scalar type");
184 assert(typeB && (typeB->isIntegerTy() || typeB->isFloatingPointTy()) &&
185 "Second Type in typePrecedence is not a scalar type");
190 if (typeA->isDoubleTy())
return typeA;
191 if (typeB->isDoubleTy())
return typeB;
193 if (typeA->isFloatTy())
return typeA;
194 if (typeB->isFloatTy())
return typeB;
196 if (typeA->isIntegerTy(64))
return typeA;
197 if (typeB->isIntegerTy(64))
return typeB;
199 if (typeA->isIntegerTy(32))
return typeA;
200 if (typeB->isIntegerTy(32))
return typeB;
202 if (typeA->isIntegerTy(16))
return typeA;
203 if (typeB->isIntegerTy(16))
return typeB;
205 if (typeA->isIntegerTy(8))
return typeA;
206 if (typeB->isIntegerTy(8))
return typeB;
208 if (typeA->isIntegerTy(1))
return typeA;
209 if (typeB->isIntegerTy(1))
return typeB;
211 assert(
false &&
"invalid LLVM type precedence");
226 const llvm::Type*
const targetType,
227 const std::string& twine =
"")
230#define BIND_ARITHMETIC_CAST_OP(Function, Twine) \
231 std::bind(&Function, \
232 std::placeholders::_1, \
233 std::placeholders::_2, \
234 std::placeholders::_3, \
237 if (targetType->isDoubleTy()) {
246 else if (targetType->isFloatTy()) {
255 else if (targetType->isHalfTy()) {
264 else if (targetType->isIntegerTy(64)) {
273 else if (targetType->isIntegerTy(32)) {
282 else if (targetType->isIntegerTy(16)) {
291 else if (targetType->isIntegerTy(8)) {
300 else if (targetType->isIntegerTy(1)) {
310#undef BIND_ARITHMETIC_CAST_OP
311 assert(
false &&
"invalid LLVM type conversion");
332 const std::string& twine =
"")
335#define BIND_BINARY_OP(Function) \
336 [twine](llvm::IRBuilder<>& B, llvm::Value* L, llvm::Value* R) \
337 -> llvm::Value* { return B.Function(L, R, twine); }
343 if (type->isFloatingPointTy()) {
346 &&
"unable to perform logical or bitwise operation on floating point values");
359 assert(
false &&
"unrecognised binary operator");
361 else if (type->isIntegerTy()) {
380 assert(
false &&
"unrecognised binary operator");
384 assert(
false &&
"invalid LLVM type for binary operation");
392 assert(from &&
"llvm Type 'from' is null in isValidCast");
393 assert(to &&
"llvm Type 'to' is null in isValidCast");
395 if ((from->isIntegerTy() || from->isFloatingPointTy()) &&
396 (to->isIntegerTy() || to->isFloatingPointTy())) {
399 if (from->isArrayTy() && to->isArrayTy()) {
400 llvm::ArrayType* af = llvm::cast<llvm::ArrayType>(from);
401 llvm::ArrayType* at = llvm::cast<llvm::ArrayType>(to);
402 if (af->getArrayNumElements() == at->getArrayNumElements()) {
404 at->getArrayElementType());
418 llvm::Type* targetType,
419 llvm::IRBuilder<>& builder)
421 assert(
value && (
value->getType()->isIntegerTy() ||
value->getType()->isFloatingPointTy()) &&
422 "First Value in arithmeticConversion is not a scalar type");
423 assert(targetType && (targetType->isIntegerTy() || targetType->isFloatingPointTy()) &&
424 "Target Type in arithmeticConversion is not a scalar type");
426 const llvm::Type*
const valueType =
value->getType();
427 if (valueType == targetType)
return value;
430 return llvmCastFunction(builder,
value, targetType);
444 llvm::Type* targetElementType,
445 llvm::IRBuilder<>& builder)
447 assert(targetElementType && (targetElementType->isIntegerTy() ||
448 targetElementType->isFloatingPointTy()) &&
449 "Target element type is not a scalar type");
450 assert(ptrToArray && ptrToArray->getType()->isPointerTy() &&
451 "Input to arrayCast is not a pointer type.");
453 llvm::Type* arrayType = ptrToArray->getType()->getContainedType(0);
454 assert(arrayType && llvm::isa<llvm::ArrayType>(arrayType));
457 llvm::Type* sourceElementType = arrayType->getArrayElementType();
458 assert(sourceElementType && (sourceElementType->isIntegerTy() ||
459 sourceElementType->isFloatingPointTy()) &&
460 "Source element type is not a scalar type");
462 if (sourceElementType == targetElementType)
return ptrToArray;
466 const size_t elementSize = arrayType->getArrayNumElements();
467 llvm::Value* targetArray =
469 llvm::ArrayType::get(targetElementType, elementSize));
471 for (
size_t i = 0; i < elementSize; ++i) {
472 llvm::Value* target = builder.CreateConstGEP2_64(targetArray, 0, i);
473 llvm::Value* source = builder.CreateConstGEP2_64(ptrToArray, 0, i);
474 source = builder.CreateLoad(source);
475 source = llvmCastFunction(builder, source, targetElementType);
476 builder.CreateStore(source, target);
492 llvm::Type* targetElementType,
493 llvm::IRBuilder<>& builder)
495 assert(targetElementType && (targetElementType->isIntegerTy() ||
496 targetElementType->isFloatingPointTy()) &&
497 "Target element type is not a scalar type");
499 llvm::Type* sourceElementType = values.front()->getType();
500 assert(sourceElementType && (sourceElementType->isIntegerTy() ||
501 sourceElementType->isFloatingPointTy()) &&
502 "Source element type is not a scalar type");
504 if (sourceElementType == targetElementType)
return;
508 for (llvm::Value*&
value : values) {
509 value = llvmCastFunction(builder,
value, targetElementType);
520 llvm::IRBuilder<>& builder)
523 for (llvm::Value*&
value : values) {
524 llvm::Type* type =
value->getType();
525 if (type->isIntegerTy() || type->isFloatingPointTy()) {
543 llvm::Value*& valueB,
544 llvm::IRBuilder<>& builder)
546 llvm::Type* type =
typePrecedence(valueA->getType(), valueB->getType());
558 llvm::IRBuilder<>& builder)
560 llvm::Type* type =
value->getType();
562 if (type->isFloatingPointTy())
return builder.CreateFCmpONE(
value, llvm::ConstantFP::get(type, 0.0));
563 else if (type->isIntegerTy(1))
return builder.CreateICmpNE(
value, llvm::ConstantInt::get(type, 0));
564 else if (type->isIntegerTy())
return builder.CreateICmpNE(
value, llvm::ConstantInt::getSigned(type, 0));
565 assert(
false &&
"Invalid type for bool conversion");
580 llvm::IRBuilder<>& builder)
582 llvm::Type* lhsType = lhs->getType();
583 assert(lhsType == rhs->getType() ||
592 lhsType = lhs->getType();
596 return llvmBinaryFunction(builder, lhs, rhs);
609 llvm::IRBuilder<>& builder)
611 return builder.CreateConstGEP2_64(ptrToArray, 0, index);
626 std::vector<llvm::Value*>& values,
627 llvm::IRBuilder<>& builder,
628 const bool loadElements =
false)
630 const size_t elements =
631 ptrToArray->getType()->getContainedType(0)->getArrayNumElements();
633 values.reserve(elements);
634 for (
size_t i = 0; i < elements; ++i) {
635 llvm::Value*
value = builder.CreateConstGEP2_64(ptrToArray, 0, i);
636 if (loadElements)
value = builder.CreateLoad(
value);
637 values.push_back(
value);
653 llvm::Value*& value1,
654 llvm::Value*& value2,
655 llvm::Value*& value3,
656 llvm::IRBuilder<>& builder)
658 assert(ptrToArray && ptrToArray->getType()->isPointerTy() &&
659 "Input to array3Unpack is not a pointer type.");
661 value1 = builder.CreateConstGEP2_64(ptrToArray, 0, 0);
662 value2 = builder.CreateConstGEP2_64(ptrToArray, 0, 1);
663 value3 = builder.CreateConstGEP2_64(ptrToArray, 0, 2);
680 llvm::IRBuilder<>& builder)
682 llvm::Type* type =
typePrecedence(value1->getType(), value2->getType());
689 llvm::Type* vectorType = llvm::ArrayType::get(type, 3);
692 llvm::Value* e1 = builder.CreateConstGEP2_64(vector, 0, 0);
693 llvm::Value* e2 = builder.CreateConstGEP2_64(vector, 0, 1);
694 llvm::Value* e3 = builder.CreateConstGEP2_64(vector, 0, 2);
696 builder.CreateStore(value1, e1);
697 builder.CreateStore(value2, e2);
698 builder.CreateStore(value3, e3);
713 llvm::IRBuilder<>& builder,
714 const size_t size = 3)
716 assert(
value && (
value->getType()->isIntegerTy() ||
717 value->getType()->isFloatingPointTy()) &&
718 "value type is not a scalar type");
720 llvm::Type* type =
value->getType();
723 llvm::ArrayType::get(type, size));
725 for (
size_t i = 0; i < size; ++i) {
726 llvm::Value* element = builder.CreateConstGEP2_64(array, 0, i);
727 builder.CreateStore(
value, element);
741 llvm::IRBuilder<>& builder)
743 llvm::Type* type = values.front()->getType();
745 llvm::ArrayType::get(type, values.size()));
748 for (llvm::Value*
const&
value : values) {
749 llvm::Value* element = builder.CreateConstGEP2_64(array, 0, idx++);
750 builder.CreateStore(
value, element);
767 llvm::IRBuilder<>& builder)
772 for (llvm::Value*
const&
value : values) {
778 for (llvm::Value*&
value : values) {
787 llvm::IRBuilder<>& builder,
788 const size_t dim = 3)
790 assert(scalar && (scalar->getType()->isIntegerTy() ||
791 scalar->getType()->isFloatingPointTy()) &&
792 "value type is not a scalar type");
794 llvm::Type* type = scalar->getType();
797 llvm::ArrayType::get(type, dim*dim));
800 for (
size_t i = 0; i < dim*dim; ++i) {
801 llvm::Value* m = ((i % (dim+1) == 0) ? scalar : zero);
802 llvm::Value* element = builder.CreateConstGEP2_64(array, 0, i);
803 builder.CreateStore(m, element);
ValueT value
Definition: GridBuilder.h:1287
Consolidated llvm types for most supported types.
@ BITOR
Definition: axparser.h:137
@ DIVIDE
Definition: axparser.h:151
@ LESSTHANOREQUAL
Definition: axparser.h:145
@ SHIFTRIGHT
Definition: axparser.h:147
@ MORETHANOREQUAL
Definition: axparser.h:144
@ EQUALSEQUALS
Definition: axparser.h:140
@ BITXOR
Definition: axparser.h:138
@ BITAND
Definition: axparser.h:139
@ AND
Definition: axparser.h:136
@ PLUS
Definition: axparser.h:148
@ LESSTHAN
Definition: axparser.h:143
@ OR
Definition: axparser.h:135
@ MODULO
Definition: axparser.h:152
@ MORETHAN
Definition: axparser.h:142
@ NOTEQUALS
Definition: axparser.h:141
@ MULTIPLY
Definition: axparser.h:150
@ SHIFTLEFT
Definition: axparser.h:146
@ MINUS
Definition: axparser.h:149
OperatorToken
Definition: Tokens.h:151
OperatorType
Definition: Tokens.h:201
@ LOGICAL
Definition: Tokens.h:203
@ BITWISE
Definition: Tokens.h:205
OperatorType operatorType(const OperatorToken token)
Definition: Tokens.h:210
void arithmeticConversion(llvm::Value *&valueA, llvm::Value *&valueB, llvm::IRBuilder<> &builder)
Chooses the highest order llvm Type as defined by typePrecedence from either of the two incoming valu...
Definition: Utils.h:542
void llvmTypeToString(const llvm::Type *const type, std::string &str)
Prints an llvm type to a std string.
Definition: Utils.h:70
llvm::Type * getBaseContainedType(llvm::Type *const type)
Return the base llvm value which is being pointed to through any number of layered pointers.
Definition: Utils.h:84
llvm::Value * arrayPackCast(std::vector< llvm::Value * > &values, llvm::IRBuilder<> &builder)
Pack a vector of loaded llvm scalar values into a new array of equal size and return a pointer to the...
Definition: Utils.h:766
llvm::Value * arrayCast(llvm::Value *ptrToArray, llvm::Type *targetElementType, llvm::IRBuilder<> &builder)
Casts an array to another array of equal size but of a different element type. Both source and target...
Definition: Utils.h:443
llvm::Constant * llvmConstant(const T t, llvm::Type *type)
Returns an llvm Constant holding a scalar value.
Definition: Types.h:328
llvm::Value * scalarToMatrix(llvm::Value *scalar, llvm::IRBuilder<> &builder, const size_t dim=3)
Definition: Utils.h:786
std::function< llvm::Value *(llvm::IRBuilder<> &, llvm::Value *, llvm::Type *)> CastFunction
Definition: Utils.h:44
llvm::Value * arrayPack(const std::vector< llvm::Value * > &values, llvm::IRBuilder<> &builder)
Pack a vector of loaded llvm scalar values into a new array of equal size and return a pointer to the...
Definition: Utils.h:740
llvm::Value * boolComparison(llvm::Value *value, llvm::IRBuilder<> &builder)
Performs a C style boolean comparison from a given scalar LLVM value.
Definition: Utils.h:557
BinaryFunction llvmBinaryConversion(const llvm::Type *const type, const ast::tokens::OperatorToken &token, const std::string &twine="")
Returns a BinaryFunction representing the corresponding instruction to perform on two scalar values,...
Definition: Utils.h:330
void arrayUnpack(llvm::Value *ptrToArray, std::vector< llvm::Value * > &values, llvm::IRBuilder<> &builder, const bool loadElements=false)
Unpack an array type into llvm Values which represent all its elements The provided llvm Value is exp...
Definition: Utils.h:625
llvm::Value * arrayIndexUnpack(llvm::Value *ptrToArray, const int16_t index, llvm::IRBuilder<> &builder)
Unpack a particular element of an array and return a pointer to that element The provided llvm Value ...
Definition: Utils.h:607
void array3Unpack(llvm::Value *ptrToArray, llvm::Value *&value1, llvm::Value *&value2, llvm::Value *&value3, llvm::IRBuilder<> &builder)
Unpack the first three elements of an array. The provided llvm Value is expected to be a pointer to a...
Definition: Utils.h:652
llvm::Type * typePrecedence(llvm::Type *const typeA, llvm::Type *const typeB)
Returns the highest order type from two LLVM Scalar types.
Definition: Utils.h:179
llvm::Value * binaryOperator(llvm::Value *lhs, llvm::Value *rhs, const ast::tokens::OperatorToken &token, llvm::IRBuilder<> &builder)
Definition: Utils.h:578
CastFunction llvmArithmeticConversion(const llvm::Type *const sourceType, const llvm::Type *const targetType, const std::string &twine="")
Returns a CastFunction which represents the corresponding instruction to convert a source llvm Type t...
Definition: Utils.h:225
void valuesToTypes(const std::vector< llvm::Value * > &values, std::vector< llvm::Type * > &types)
Populate a vector of llvm Types from a vector of llvm values.
Definition: Utils.h:55
llvm::Value * insertStaticAlloca(llvm::IRBuilder<> &B, llvm::Type *type, llvm::Value *size=nullptr)
Insert a stack allocation at the beginning of the current function of the provided type and size....
Definition: Utils.h:123
std::function< llvm::Value *(llvm::IRBuilder<> &, llvm::Value *, llvm::Value *)> BinaryFunction
Definition: Utils.h:47
llvm::Value * llvmPointerFromAddress(const ValueT *const &ptr, llvm::IRBuilder<> &builder)
Return an llvm value representing a pointer to the provided ptr builtin ValueT.
Definition: Utils.h:104
llvm::Value * array3Pack(llvm::Value *value1, llvm::Value *value2, llvm::Value *value3, llvm::IRBuilder<> &builder)
Pack three values into a new array and return a pointer to the newly allocated array....
Definition: Utils.h:677
bool isValidCast(llvm::Type *from, llvm::Type *to)
Returns true if the llvm Type 'from' can be safely cast to the llvm Type 'to'.
Definition: Utils.h:390
llvm::Argument * extractArgument(llvm::Function *F, const std::string &name)
Definition: Utils.h:163
Definition: Exceptions.h:13
#define BIND_ARITHMETIC_CAST_OP(Function, Twine)
#define BIND_BINARY_OP(Function)
LLVM type mapping from pod types.
Definition: Types.h:55
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202