| 1 |
//===- LLVMTypeTest.cpp - Tests for LLVM types ----------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "LLVMTestBase.h" |
| 10 |
#include "mlir/Dialect/LLVMIR/LLVMTypes.h" |
| 11 |
#include "mlir/IR/SubElementInterfaces.h" |
| 12 |
|
| 13 |
using namespace mlir; |
| 14 |
using namespace mlir::LLVM; |
| 15 |
|
| 16 |
TEST_F(LLVMIRTest, IsStructTypeMutable) { |
| 17 |
auto structTy = LLVMStructType::getIdentified(&context, "foo"); |
| 18 |
ASSERT_TRUE(bool(structTy)); |
| 19 |
ASSERT_TRUE(structTy.hasTrait<TypeTrait::IsMutable>()); |
| 20 |
} |
| 21 |
|
| 22 |
TEST_F(LLVMIRTest, MutualReferencedSubElementTypes) { |
| 23 |
auto fooStructTy = LLVMStructType::getIdentified(&context, "foo"); |
| 24 |
ASSERT_TRUE(bool(fooStructTy)); |
| 25 |
auto barStructTy = LLVMStructType::getIdentified(&context, "bar"); |
| 26 |
ASSERT_TRUE(bool(barStructTy)); |
| 27 |
|
| 28 |
// Created two structs that are referencing each other. |
| 29 |
Type fooBody[] = {LLVMPointerType::get(barStructTy)}; |
| 30 |
ASSERT_TRUE(succeeded(fooStructTy.setBody(fooBody, /*isPacked=*/false))); |
| 31 |
Type barBody[] = {LLVMPointerType::get(fooStructTy)}; |
| 32 |
ASSERT_TRUE(succeeded(barStructTy.setBody(barBody, /*isPacked=*/false))); |
| 33 |
|
| 34 |
auto subElementInterface = fooStructTy.dyn_cast<SubElementTypeInterface>(); |
| 35 |
ASSERT_TRUE(bool(subElementInterface)); |
| 36 |
// Test if walkSubElements goes into infinite loops. |
| 37 |
SmallVector<Type, 4> subElementTypes; |
| 38 |
subElementInterface.walkSubElements( |
| 39 |
[](Attribute attr) {}, |
| 40 |
[&](Type type) { subElementTypes.push_back(type); }); |
| 41 |
// We don't record LLVMPointerType (because it's immutable), thus |
| 42 |
// !llvm.ptr<struct<"bar",...>> will be visited twice. |
| 43 |
ASSERT_EQ(subElementTypes.size(), 5U); |
| 44 |
|
| 45 |
// !llvm.ptr<struct<"bar",...>> |
| 46 |
ASSERT_TRUE(subElementTypes[0].isa<LLVMPointerType>()); |
| 47 |
|
| 48 |
// !llvm.struct<"foo",...> |
| 49 |
auto structType = subElementTypes[1].dyn_cast<LLVMStructType>(); |
| 50 |
ASSERT_TRUE(bool(structType)); |
| 51 |
ASSERT_TRUE(structType.getName().equals("foo")); |
| 52 |
|
| 53 |
// !llvm.ptr<struct<"foo",...>> |
| 54 |
ASSERT_TRUE(subElementTypes[2].isa<LLVMPointerType>()); |
| 55 |
|
| 56 |
// !llvm.struct<"bar",...> |
| 57 |
structType = subElementTypes[3].dyn_cast<LLVMStructType>(); |
| 58 |
ASSERT_TRUE(bool(structType)); |
| 59 |
ASSERT_TRUE(structType.getName().equals("bar")); |
| 60 |
|
| 61 |
// !llvm.ptr<struct<"bar",...>> |
| 62 |
ASSERT_TRUE(subElementTypes[4].isa<LLVMPointerType>()); |
| 63 |
} |