Changeset View
Changeset View
Standalone View
Standalone View
mlir/lib/Dialect/LLVMIR/IR/LLVMTypeSyntax.cpp
//===- LLVMTypeSyntax.cpp - Parsing/printing for MLIR LLVM Dialect types --===// | //===- LLVMTypeSyntax.cpp - Parsing/printing for MLIR LLVM Dialect types --===// | ||||
// | // | ||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
// See https://llvm.org/LICENSE.txt for license information. | // See https://llvm.org/LICENSE.txt for license information. | ||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
// | // | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
#include "mlir/Dialect/LLVMIR/LLVMTypes.h" | #include "mlir/Dialect/LLVMIR/LLVMTypes.h" | ||||
#include "mlir/IR/Builders.h" | #include "mlir/IR/Builders.h" | ||||
#include "mlir/IR/DialectImplementation.h" | #include "mlir/IR/DialectImplementation.h" | ||||
#include "llvm/ADT/SetVector.h" | #include "llvm/ADT/SetVector.h" | ||||
#include "llvm/ADT/TypeSwitch.h" | |||||
using namespace mlir; | using namespace mlir; | ||||
using namespace mlir::LLVM; | using namespace mlir::LLVM; | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
// Printing. | // Printing. | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
static void printTypeImpl(llvm::raw_ostream &os, LLVMType type, | static void printTypeImpl(llvm::raw_ostream &os, LLVMType type, | ||||
llvm::SetVector<StringRef> &stack); | llvm::SetVector<StringRef> &stack); | ||||
/// Returns the keyword to use for the given type. | /// Returns the keyword to use for the given type. | ||||
static StringRef getTypeKeyword(LLVMType type) { | static StringRef getTypeKeyword(LLVMType type) { | ||||
switch (type.getKind()) { | return TypeSwitch<Type, StringRef>(type) | ||||
case LLVMType::VoidType: | .Case<LLVMVoidType>([&](Type) { return "void"; }) | ||||
return "void"; | .Case<LLVMHalfType>([&](Type) { return "half"; }) | ||||
case LLVMType::HalfType: | .Case<LLVMBFloatType>([&](Type) { return "bfloat"; }) | ||||
return "half"; | .Case<LLVMFloatType>([&](Type) { return "float"; }) | ||||
case LLVMType::BFloatType: | .Case<LLVMDoubleType>([&](Type) { return "double"; }) | ||||
return "bfloat"; | .Case<LLVMFP128Type>([&](Type) { return "fp128"; }) | ||||
case LLVMType::FloatType: | .Case<LLVMX86FP80Type>([&](Type) { return "x86_fp80"; }) | ||||
return "float"; | .Case<LLVMPPCFP128Type>([&](Type) { return "ppc_fp128"; }) | ||||
case LLVMType::DoubleType: | .Case<LLVMX86MMXType>([&](Type) { return "x86_mmx"; }) | ||||
return "double"; | .Case<LLVMTokenType>([&](Type) { return "token"; }) | ||||
case LLVMType::FP128Type: | .Case<LLVMLabelType>([&](Type) { return "label"; }) | ||||
return "fp128"; | .Case<LLVMMetadataType>([&](Type) { return "metadata"; }) | ||||
case LLVMType::X86FP80Type: | .Case<LLVMFunctionType>([&](Type) { return "func"; }) | ||||
return "x86_fp80"; | .Case<LLVMIntegerType>([&](Type) { return "i"; }) | ||||
case LLVMType::PPCFP128Type: | .Case<LLVMPointerType>([&](Type) { return "ptr"; }) | ||||
return "ppc_fp128"; | .Case<LLVMVectorType>([&](Type) { return "vec"; }) | ||||
case LLVMType::X86MMXType: | .Case<LLVMArrayType>([&](Type) { return "array"; }) | ||||
return "x86_mmx"; | .Case<LLVMStructType>([&](Type) { return "struct"; }) | ||||
case LLVMType::TokenType: | .Default([](Type) -> StringRef { | ||||
return "token"; | llvm_unreachable("unexpected 'llvm' type kind"); | ||||
case LLVMType::LabelType: | }); | ||||
return "label"; | |||||
case LLVMType::MetadataType: | |||||
return "metadata"; | |||||
case LLVMType::FunctionType: | |||||
return "func"; | |||||
case LLVMType::IntegerType: | |||||
return "i"; | |||||
case LLVMType::PointerType: | |||||
return "ptr"; | |||||
case LLVMType::FixedVectorType: | |||||
case LLVMType::ScalableVectorType: | |||||
return "vec"; | |||||
case LLVMType::ArrayType: | |||||
return "array"; | |||||
case LLVMType::StructType: | |||||
return "struct"; | |||||
} | |||||
llvm_unreachable("unhandled type kind"); | |||||
} | } | ||||
/// Prints the body of a structure type. Uses `stack` to avoid printing | /// Prints the body of a structure type. Uses `stack` to avoid printing | ||||
/// recursive structs indefinitely. | /// recursive structs indefinitely. | ||||
static void printStructTypeBody(llvm::raw_ostream &os, LLVMStructType type, | static void printStructTypeBody(llvm::raw_ostream &os, LLVMStructType type, | ||||
llvm::SetVector<StringRef> &stack) { | llvm::SetVector<StringRef> &stack) { | ||||
if (type.isIdentified() && type.isOpaque()) { | if (type.isIdentified() && type.isOpaque()) { | ||||
os << "opaque"; | os << "opaque"; | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | |||||
/// note that "b" is printed twice. | /// note that "b" is printed twice. | ||||
static void printTypeImpl(llvm::raw_ostream &os, LLVMType type, | static void printTypeImpl(llvm::raw_ostream &os, LLVMType type, | ||||
llvm::SetVector<StringRef> &stack) { | llvm::SetVector<StringRef> &stack) { | ||||
if (!type) { | if (!type) { | ||||
os << "<<NULL-TYPE>>"; | os << "<<NULL-TYPE>>"; | ||||
return; | return; | ||||
} | } | ||||
unsigned kind = type.getKind(); | |||||
os << getTypeKeyword(type); | os << getTypeKeyword(type); | ||||
// Trivial types only consist of their keyword. | |||||
if (LLVMType::FIRST_TRIVIAL_TYPE <= kind && | |||||
kind <= LLVMType::LAST_TRIVIAL_TYPE) | |||||
return; | |||||
if (auto intType = type.dyn_cast<LLVMIntegerType>()) { | if (auto intType = type.dyn_cast<LLVMIntegerType>()) { | ||||
os << intType.getBitWidth(); | os << intType.getBitWidth(); | ||||
return; | return; | ||||
} | } | ||||
if (auto ptrType = type.dyn_cast<LLVMPointerType>()) { | if (auto ptrType = type.dyn_cast<LLVMPointerType>()) { | ||||
os << '<'; | os << '<'; | ||||
printTypeImpl(os, ptrType.getElementType(), stack); | printTypeImpl(os, ptrType.getElementType(), stack); | ||||
Show All 13 Lines | if (auto vectorType = type.dyn_cast<LLVMScalableVectorType>()) { | ||||
printTypeImpl(os, vectorType.getElementType(), stack); | printTypeImpl(os, vectorType.getElementType(), stack); | ||||
os << '>'; | os << '>'; | ||||
return; | return; | ||||
} | } | ||||
if (auto structType = type.dyn_cast<LLVMStructType>()) | if (auto structType = type.dyn_cast<LLVMStructType>()) | ||||
return printStructType(os, structType, stack); | return printStructType(os, structType, stack); | ||||
printFunctionType(os, type.cast<LLVMFunctionType>(), stack); | if (auto funcType = type.dyn_cast<LLVMFunctionType>()) | ||||
return printFunctionType(os, funcType, stack); | |||||
} | } | ||||
void mlir::LLVM::detail::printType(LLVMType type, DialectAsmPrinter &printer) { | void mlir::LLVM::detail::printType(LLVMType type, DialectAsmPrinter &printer) { | ||||
llvm::SetVector<StringRef> stack; | llvm::SetVector<StringRef> stack; | ||||
return printTypeImpl(printer.getStream(), type, stack); | return printTypeImpl(printer.getStream(), type, stack); | ||||
} | } | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
▲ Show 20 Lines • Show All 291 Lines • Show Last 20 Lines |