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 "TestDialect.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>> { MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID__anon89bf08570111::TestDataLayoutQuery24 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestDataLayoutQuery) 25 26 StringRef getArgument() const final { return "test-data-layout-query"; } getDescription__anon89bf08570111::TestDataLayoutQuery27 StringRef getDescription() const final { return "Test data layout queries"; } runOnOperation__anon89bf08570111::TestDataLayoutQuery28 void runOnOperation() override { 29 func::FuncOp func = getOperation(); 30 Builder builder(func.getContext()); 31 const DataLayoutAnalysis &layouts = getAnalysis<DataLayoutAnalysis>(); 32 33 func.walk([&](test::DataLayoutQueryOp op) { 34 // Skip the ops with already processed in a deeper call. 35 if (op->getAttr("size")) 36 return; 37 38 const DataLayout &layout = layouts.getAbove(op); 39 unsigned size = layout.getTypeSize(op.getType()); 40 unsigned bitsize = layout.getTypeSizeInBits(op.getType()); 41 unsigned alignment = layout.getTypeABIAlignment(op.getType()); 42 unsigned preferred = layout.getTypePreferredAlignment(op.getType()); 43 op->setAttrs( 44 {builder.getNamedAttr("size", builder.getIndexAttr(size)), 45 builder.getNamedAttr("bitsize", builder.getIndexAttr(bitsize)), 46 builder.getNamedAttr("alignment", builder.getIndexAttr(alignment)), 47 builder.getNamedAttr("preferred", builder.getIndexAttr(preferred))}); 48 }); 49 } 50 }; 51 } // namespace 52 53 namespace mlir { 54 namespace test { registerTestDataLayoutQuery()55void registerTestDataLayoutQuery() { PassRegistration<TestDataLayoutQuery>(); } 56 } // namespace test 57 } // namespace mlir 58