[mlir][llvm] Fix bug in the LLVM IR constant import.
authorTobias Gysi <tobias.gysi@nextsilicon.com>
Wed, 14 Dec 2022 09:27:01 +0000 (10:27 +0100)
committerTobias Gysi <tobias.gysi@nextsilicon.com>
Wed, 14 Dec 2022 09:27:12 +0000 (10:27 +0100)
The recently introduced iterative constant import
(https://reviews.llvm.org/D137559) fails for programs that
subsequently import constant expressions with duplicate
subexpressions. The reason is a broken duplicate check
in getConstantsToConvert. The revision fixes the bug and
adds a test case that imports two constant expressions
with duplicates.

Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D139918

mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
mlir/test/Target/LLVMIR/Import/constant.ll

index c8f9dab..ec9bde2 100644 (file)
@@ -688,7 +688,7 @@ Importer::getConstantsToConvert(llvm::Constant *constant) {
   while (!workList.empty()) {
     llvm::Constant *current = workList.pop_back_val();
     // Skip constants that have been converted before and store all other ones.
-    if (valueMapping.count(constant))
+    if (valueMapping.count(current))
       continue;
     orderedList.push_back(current);
     // Add the current constant's dependencies to the work list. Only add
index e1889d7..98bd8e8 100644 (file)
@@ -208,3 +208,21 @@ define i32 @function_address_after_def() {
 ; CHECK:  %[[CHAIN1:.+]] = llvm.insertelement %[[NULL]], %[[CHAIN0]][%[[P1]] : i32] : !llvm.vec<2 x ptr<struct<"simple_agg_type", (i32, i8, i16, i32)>>>
 ; CHECK:  llvm.return %[[CHAIN1]] : !llvm.vec<2 x ptr<struct<"simple_agg_type", (i32, i8, i16, i32)>>>
 @vector_agg = global <2 x %simple_agg_type*> <%simple_agg_type* null, %simple_agg_type* null>
+
+; // -----
+
+; Verfiy the import of subsequent constant expressions with duplicates.
+
+@global = external global i32, align 8
+
+; CHECK-LABEL: @const_exprs_with_duplicate
+define i64 @const_exprs_with_duplicate() {
+  ; CHECK: %[[ADDR:.+]] = llvm.mlir.addressof @global : !llvm.ptr<i32>
+  ; CHECK: llvm.getelementptr %[[ADDR]][%{{.*}}] : (!llvm.ptr<i32>, i32) -> !llvm.ptr<i32>
+  %1 = add i64 1, ptrtoint (i32* getelementptr (i32, i32* @global, i32 7) to i64)
+
+  ; Verify the address value is reused.
+  ; CHECK: llvm.getelementptr %[[ADDR]][%{{.*}}] : (!llvm.ptr<i32>, i32) -> !llvm.ptr<i32>
+  %2 = add i64 %1, ptrtoint (i32* getelementptr (i32, i32* @global, i32 42) to i64)
+  ret i64 %2
+}