Skip to content

Commit

Permalink
[gccjit] add translation support for some builtin types
Browse files Browse the repository at this point in the history
  • Loading branch information
Lancern committed Nov 19, 2024
1 parent d86478f commit 790e680
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/Translation/TranslateToGCCJIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include <algorithm>
#include <cstddef>
#include <initializer_list>
#include <iterator>
#include <string>
#include <utility>
Expand Down
48 changes: 47 additions & 1 deletion src/Translation/TypeTranslation.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "mlir-gccjit/Translation/TranslateToGCCJIT.h"

#include <climits>
#include <optional>

#include <llvm/ADT/TypeSwitch.h>
Expand All @@ -8,6 +9,7 @@
#include <libgccjit.h>

#include "mlir-gccjit/IR/GCCJITTypes.h"
#include "llvm/Support/ErrorHandling.h"

namespace mlir::gccjit {

Expand Down Expand Up @@ -97,14 +99,49 @@ gcc_jit_type *GCCJITTranslation::convertType(mlir::Type type) {
auto kind = t.getKind();
return gcc_jit_context_get_type(ctxt, kind);
})
.Case([&](mlir::IntegerType t) {
assert(!t.isSignless() && "signless integer type is not supported");
return gcc_jit_context_get_int_type(ctxt, t.getWidth() / CHAR_BIT,
t.isSigned());
})
.Case([&](mlir::IndexType) {
return gcc_jit_context_get_type(ctxt, GCC_JIT_TYPE_SIZE_T);
})
.Case([&](gccjit::FloatType t) {
auto kind = t.getKind();
return gcc_jit_context_get_type(ctxt, kind);
})
.Case([&](mlir::Float32Type) {
return gcc_jit_context_get_type(ctxt, GCC_JIT_TYPE_FLOAT);
})
.Case([&](mlir::Float64Type) {
return gcc_jit_context_get_type(ctxt, GCC_JIT_TYPE_DOUBLE);
})
.Case([&](gccjit::ComplexType t) {
auto kind = t.getKind();
return gcc_jit_context_get_type(ctxt, kind);
})
.Case([&](mlir::ComplexType t) {
mlir::Type elementTy = t.getElementType();
if (mlir::isa<mlir::Float32Type>(elementTy))
return gcc_jit_context_get_type(ctxt, GCC_JIT_TYPE_COMPLEX_FLOAT);
if (mlir::isa<mlir::Float64Type>(elementTy))
return gcc_jit_context_get_type(ctxt,
GCC_JIT_TYPE_COMPLEX_DOUBLE);
if (auto floatTy = mlir::dyn_cast<gccjit::FloatType>(elementTy)) {
switch (floatTy.getKind()) {
case GCC_JIT_TYPE_FLOAT:
return gcc_jit_context_get_type(ctxt,
GCC_JIT_TYPE_COMPLEX_FLOAT);
case GCC_JIT_TYPE_DOUBLE:
return gcc_jit_context_get_type(ctxt,
GCC_JIT_TYPE_COMPLEX_DOUBLE);
default:
break;
}
}
llvm_unreachable("unsupported complex element type");
})
.Case([&](gccjit::VoidType t) {
return gcc_jit_context_get_type(ctxt, GCC_JIT_TYPE_VOID);
})
Expand All @@ -119,14 +156,23 @@ gcc_jit_type *GCCJITTranslation::convertType(mlir::Type type) {
auto size = t.getNumUnits();
return gcc_jit_type_get_vector(elemType, size);
})
.Case([&](mlir::VectorType t) {
assert(!t.isScalable() &&
"scalable vector types are not supported");
auto *elemType = convertType(t.getElementType());
auto size = t.getNumElements();
return gcc_jit_type_get_vector(elemType, size);
})
.Case([&](gccjit::StructType t) -> gcc_jit_type * {
gcc_jit_struct *rawType = getOrCreateStructEntry(t).getRawHandle();
return gcc_jit_struct_as_type(rawType);
})
.Case([&](gccjit::UnionType t) -> gcc_jit_type * {
return getOrCreateUnionEntry(t).getRawHandle();
})
.Default([](mlir::Type) { return nullptr; });
.Default([](mlir::Type) -> gcc_jit_type * {
llvm_unreachable("unsupported type for gccjit translation");
});
typeMap[type] = res;
return res;
}
Expand Down

0 comments on commit 790e680

Please sign in to comment.