| 1 | //===- TestDataLayoutQuery.cpp - Test Data Layout Queries -----------------===// |
| 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 "TestOpsInterfaces.h" |
| 10 | #include "mlir/Analysis/DataLayoutAnalysis.h" |
| 11 | #include "mlir/Dialect/DLTI/DLTI.h" |
| 12 | #include "mlir/IR/BuiltinAttributes.h" |
| 13 | #include "mlir/Pass/Pass.h" |
| 14 | |
| 15 | using namespace mlir; |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | /// A pass that finds "test.data_layout_query" operations and attaches to them |
| 20 | /// attributes containing the results of data layout queries for operation |
| 21 | /// result types. |
| 22 | struct TestDataLayoutQuery |
| 23 | : public PassWrapper<TestDataLayoutQuery, OperationPass<func::FuncOp>> { |
| 24 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestDataLayoutQuery) |
| 25 | |
| 26 | StringRef getArgument() const final { return "test-data-layout-query" ; } |
| 27 | StringRef getDescription() const final { return "Test data layout queries" ; } |
| 28 | void runOnOperation() override { |
| 29 | func::FuncOp func = getOperation(); |
| 30 | Builder builder(func.getContext()); |
| 31 | const DataLayoutAnalysis &layouts = getAnalysis<DataLayoutAnalysis>(); |
| 32 | |
| 33 | func.walk(callback: [&](test::DataLayoutQueryOp op) { |
| 34 | // Skip the ops with already processed in a deeper call. |
| 35 | if (op->getDiscardableAttr("size" )) |
| 36 | return; |
| 37 | |
| 38 | const DataLayout &layout = layouts.getAbove(operation: op); |
| 39 | llvm::TypeSize size = layout.getTypeSize(t: op.getType()); |
| 40 | llvm::TypeSize bitsize = layout.getTypeSizeInBits(t: op.getType()); |
| 41 | llvm::TypeSize allocSize = layout.getTypeAllocSize(t: op.getType()); |
| 42 | uint64_t alignment = layout.getTypeABIAlignment(t: op.getType()); |
| 43 | uint64_t preferred = layout.getTypePreferredAlignment(t: op.getType()); |
| 44 | uint64_t index = layout.getTypeIndexBitwidth(t: op.getType()).value_or(0); |
| 45 | Attribute endianness = layout.getEndianness(); |
| 46 | Attribute defaultMemorySpace = layout.getDefaultMemorySpace(); |
| 47 | Attribute allocaMemorySpace = layout.getAllocaMemorySpace(); |
| 48 | Attribute manglingMode = layout.getManglingMode(); |
| 49 | Attribute programMemorySpace = layout.getProgramMemorySpace(); |
| 50 | Attribute globalMemorySpace = layout.getGlobalMemorySpace(); |
| 51 | uint64_t stackAlignment = layout.getStackAlignment(); |
| 52 | Attribute functionPointerAlignment = layout.getFunctionPointerAlignment(); |
| 53 | Attribute legalIntWidths = layout.getLegalIntWidths(); |
| 54 | |
| 55 | auto convertTypeSizeToAttr = [&](llvm::TypeSize typeSize) -> Attribute { |
| 56 | if (!typeSize.isScalable()) |
| 57 | return builder.getIndexAttr(value: typeSize); |
| 58 | |
| 59 | return builder.getDictionaryAttr(value: { |
| 60 | builder.getNamedAttr(name: "scalable" , val: builder.getUnitAttr()), |
| 61 | builder.getNamedAttr( |
| 62 | name: "minimal_size" , |
| 63 | val: builder.getIndexAttr(value: typeSize.getKnownMinValue())), |
| 64 | }); |
| 65 | }; |
| 66 | |
| 67 | op->setAttrs( |
| 68 | {builder.getNamedAttr(name: "size" , val: convertTypeSizeToAttr(size)), |
| 69 | builder.getNamedAttr(name: "bitsize" , val: convertTypeSizeToAttr(bitsize)), |
| 70 | builder.getNamedAttr(name: "alloc_size" , val: convertTypeSizeToAttr(allocSize)), |
| 71 | builder.getNamedAttr(name: "alignment" , val: builder.getIndexAttr(value: alignment)), |
| 72 | builder.getNamedAttr(name: "preferred" , val: builder.getIndexAttr(value: preferred)), |
| 73 | builder.getNamedAttr(name: "index" , val: builder.getIndexAttr(value: index)), |
| 74 | builder.getNamedAttr(name: "endianness" , val: endianness == Attribute() |
| 75 | ? builder.getStringAttr(bytes: "" ) |
| 76 | : endianness), |
| 77 | builder.getNamedAttr(name: "default_memory_space" , |
| 78 | val: defaultMemorySpace == Attribute() |
| 79 | ? builder.getUI32IntegerAttr(value: 0) |
| 80 | : defaultMemorySpace), |
| 81 | builder.getNamedAttr(name: "alloca_memory_space" , |
| 82 | val: allocaMemorySpace == Attribute() |
| 83 | ? builder.getUI32IntegerAttr(value: 0) |
| 84 | : allocaMemorySpace), |
| 85 | builder.getNamedAttr(name: "mangling_mode" , val: manglingMode == Attribute() |
| 86 | ? builder.getStringAttr(bytes: "" ) |
| 87 | : manglingMode), |
| 88 | builder.getNamedAttr(name: "program_memory_space" , |
| 89 | val: programMemorySpace == Attribute() |
| 90 | ? builder.getUI32IntegerAttr(value: 0) |
| 91 | : programMemorySpace), |
| 92 | builder.getNamedAttr(name: "global_memory_space" , |
| 93 | val: globalMemorySpace == Attribute() |
| 94 | ? builder.getUI32IntegerAttr(value: 0) |
| 95 | : globalMemorySpace), |
| 96 | builder.getNamedAttr(name: "stack_alignment" , |
| 97 | val: builder.getIndexAttr(value: stackAlignment)), |
| 98 | builder.getNamedAttr(name: "function_pointer_alignment" , |
| 99 | val: functionPointerAlignment == Attribute() |
| 100 | ? FunctionPointerAlignmentAttr::get( |
| 101 | context: builder.getContext(), alignment: 0, |
| 102 | /*function_dependent=*/false) |
| 103 | : functionPointerAlignment), |
| 104 | builder.getNamedAttr(name: "legal_int_widths" , |
| 105 | val: legalIntWidths == Attribute() |
| 106 | ? builder.getDenseI32ArrayAttr(values: {}) |
| 107 | : legalIntWidths) |
| 108 | |
| 109 | }); |
| 110 | }); |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | struct TestDLTIQueryOpInterface |
| 115 | : public PassWrapper<TestDLTIQueryOpInterface, OperationPass<>> { |
| 116 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestDLTIQueryOpInterface) |
| 117 | |
| 118 | StringRef getArgument() const final { return "test-dlti-query-op-interface" ; } |
| 119 | StringRef getDescription() const final { |
| 120 | return "Test the DLTI query operation interface" ; |
| 121 | } |
| 122 | |
| 123 | void runOnOperation() override { |
| 124 | WalkResult result = getOperation()->walk(callback: [&](Operation *op) { |
| 125 | auto action = |
| 126 | op->getDiscardableAttrOfType<StringAttr>(name: "test.dlti_action" ); |
| 127 | if (!action) |
| 128 | return WalkResult::advance(); |
| 129 | |
| 130 | auto queryOp = dyn_cast<DLTIQueryOpInterface>(Val: op); |
| 131 | if (!queryOp) { |
| 132 | op->emitError(message: "expected DLTIQueryOpInterface" ); |
| 133 | return WalkResult::interrupt(); |
| 134 | } |
| 135 | |
| 136 | Builder builder(op->getContext()); |
| 137 | auto key = [&](StringRef value) { return builder.getStringAttr(bytes: value); }; |
| 138 | if (action.getValue() == "update" ) { |
| 139 | if (failed(Result: queryOp.setDlti(key: key("inserted" ), |
| 140 | value: builder.getI32IntegerAttr(value: 1))) || |
| 141 | failed(Result: queryOp.setDlti(key: key("replaced" ), |
| 142 | value: builder.getI32IntegerAttr(value: 2))) || |
| 143 | failed(Result: queryOp.setDlti(key: builder.getI32Type(), |
| 144 | value: builder.getI32IntegerAttr(value: 32))) || |
| 145 | failed(Result: queryOp.setDlti(key: key("removed" ), value: Attribute())) || |
| 146 | failed(Result: queryOp.setDlti(key: key("absent" ), value: Attribute()))) { |
| 147 | op->emitError(message: "failed to update DLTI map" ); |
| 148 | return WalkResult::interrupt(); |
| 149 | } |
| 150 | } else if (action.getValue() == "clear" ) { |
| 151 | if (failed(Result: queryOp.setDlti(key: key("only" ), value: Attribute())) || |
| 152 | failed(Result: queryOp.setDlti(key: key("absent" ), value: Attribute()))) { |
| 153 | op->emitError(message: "failed to clear DLTI map" ); |
| 154 | return WalkResult::interrupt(); |
| 155 | } |
| 156 | } else if (action.getValue() == "fail" ) { |
| 157 | if (succeeded( |
| 158 | Result: queryOp.setDlti(key: key("new" ), value: builder.getI32IntegerAttr(value: 1)))) { |
| 159 | op->emitError(message: "unexpectedly updated immutable DLTI representation" ); |
| 160 | return WalkResult::interrupt(); |
| 161 | } |
| 162 | op->setDiscardableAttr(name: "test.set_dlti_failed" , value: builder.getUnitAttr()); |
| 163 | } else if (action.getValue() == "invalid" ) { |
| 164 | Attribute value = builder.getI32IntegerAttr(value: 1); |
| 165 | if (succeeded(Result: queryOp.setDlti(key: key("" ), value)) || |
| 166 | succeeded(Result: queryOp.setDlti(key: DataLayoutEntryKey(), value)) || |
| 167 | succeeded(Result: queryOp.setDlti(key: key("" ), value: Attribute()))) { |
| 168 | op->emitError(message: "unexpectedly accepted an invalid DLTI key" ); |
| 169 | return WalkResult::interrupt(); |
| 170 | } |
| 171 | } |
| 172 | op->removeDiscardableAttr(name: "test.dlti_action" ); |
| 173 | return WalkResult::advance(); |
| 174 | }); |
| 175 | if (result.wasInterrupted()) |
| 176 | signalPassFailure(); |
| 177 | } |
| 178 | }; |
| 179 | } // namespace |
| 180 | |
| 181 | namespace mlir { |
| 182 | namespace test { |
| 183 | void registerTestDataLayoutQuery() { |
| 184 | PassRegistration<TestDataLayoutQuery>(); |
| 185 | PassRegistration<TestDLTIQueryOpInterface>(); |
| 186 | } |
| 187 | } // namespace test |
| 188 | } // namespace mlir |
| 189 | |