The following code snippit shows 3 different approaches for creating a mlir::RankedTensorType
. When running the program using -fsanitize=address
, the first two methods work correctly. The third method produces a stack-use-after-scope
violation (shown later).
Is the 3rd method valid?
#include <cassert>
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Builders.h"
int main(int argc, char* argv[]) {
mlir::MLIRContext context;
mlir::OpBuilder builder{&context};
llvm::SmallVector<int64_t> vec{1, 2, 3};
llvm::ArrayRef<int64_t> shape_vec{vec};
auto t1 = mlir::RankedTensorType::get(shape_vec, builder.getF32Type());
assert(isa<mlir::RankedTensorType>(t1));
auto t2 = mlir::RankedTensorType::get(llvm::ArrayRef<int64_t>{1, 2, 3}, builder.getF32Type());
assert(isa<mlir::RankedTensorType>(t2));
llvm::ArrayRef<int64_t> shape_il{{1, 2, 3}};
auto t3 = mlir::RankedTensorType::get(shape_il, builder.getF32Type());
assert(isa<mlir::RankedTensorType>(t3));
return 0;
}
Here is the partial stack trace of the ASAN failure (and yes, I know it is unusual to use GCC 12.3 to compile a project that leverages LLVM 17.0.2, but that shouldn't change whether the original code is valid)
==56088==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7ffc4efaad00 at pc 0x7eff62d41ba0 bp 0x7ffc4efaa720 sp 0x7ffc4efa9ed0
READ of size 24 at 0x7ffc4efaad00 thread T0
#0 0x7eff62d41b9f in MemcmpInterceptorCommon(void*, int (*)(void const*, void const*, unsigned long), void const*, void const*, unsigned long) ../../../../libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:861
#1 0x7eff62d422d8 in __interceptor_memcmp ../../../../libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:892
#2 0x7eff62d422d8 in __interceptor_memcmp ../../../../libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:887
#3 0x7eff614c9539 in int std::__memcmp<long, long>(long const*, long const*, unsigned long) /software/tools/gcc/12.3.0/include/c++/12.3.0/bits/stl_algobase.h:105
#4 0x7eff614c9539 in bool std::__equal<true>::equal<long>(long const*, long const*, long const*) /software/tools/gcc/12.3.0/include/c++/12.3.0/bits/stl_algobase.h:1176
#5 0x7eff614c9539 in bool std::__equal_aux1<long const*, long const*>(long const*, long const*, long const*) /software/tools/gcc/12.3.0/include/c++/12.3.0/bits/stl_algobase.h:1210
#6 0x7eff614c9539 in bool std::__equal_aux<long const*, long const*>(long const*, long const*, long const*) /software/tools/gcc/12.3.0/include/c++/12.3.0/bits/stl_algobase.h:1218
#7 0x7eff614c9539 in bool std::equal<long const*, long const*>(long const*, long const*, long const*) /software/tools/gcc/12.3.0/include/c++/12.3.0/bits/stl_algobase.h:1555
#8 0x7eff614c9539 in llvm::ArrayRef<long>::equals(llvm::ArrayRef<long>) const /software/tools/llvm/17.0.2/llvm-project/llvm/include/llvm/ADT/ArrayRef.h:188
#9 0x7eff614c9539 in bool llvm::operator==<long>(llvm::ArrayRef<long>, llvm::ArrayRef<long>) /software/tools/llvm/17.0.2/llvm-project/llvm/include/llvm/ADT/ArrayRef.h:669
#10 0x7eff614c9539 in mlir::detail::RankedTensorTypeStorage::operator==(std::tuple<llvm::ArrayRef<long>, mlir::Type, mlir::Attribute> const&) const tools/mlir/include/mlir/IR/BuiltinTypes.cpp.inc:239
#11 0x7eff614c9539 in mlir::StorageUniquer::get<mlir::detail::RankedTensorTypeStorage, llvm::ArrayRef<long>&, mlir::Type&, mlir::Attribute&>(llvm::function_ref<void (mlir::detail::RankedTensorTypeStorage*)>, mlir::TypeID, llvm::ArrayRef<long>&, mlir::Type&, mlir::Attribute&)::{lambda(mlir::StorageUniquer::BaseStorage const*)#1}::operator()(mlir::StorageUniquer::BaseStorage const*) const /software/tools/llvm/17.0.2/llvm-project/mlir/include/mlir/Support/StorageUniquer.h:205