[MemRefToLLVM][NFC] Use early exit for the getter of the buffer ptr
authorQuentin Colombet <quentin.colombet@gmail.com>
Tue, 16 May 2023 08:54:25 +0000 (10:54 +0200)
committerQuentin Colombet <quentin.colombet@gmail.com>
Tue, 16 May 2023 08:54:25 +0000 (10:54 +0200)
Address review comment from https://reviews.llvm.org/D148947

mlir/lib/Conversion/LLVMCommon/MemRefBuilder.cpp

index df9dafc..2c9580e 100644 (file)
@@ -205,17 +205,19 @@ Value MemRefDescriptor::bufferPtr(OpBuilder &builder, Location loc,
   auto [strides, offsetCst] = getStridesAndOffset(type);
 
   Value ptr = alignedPtr(builder, loc);
-  // Skip if offset is zero.
-  if (offsetCst != 0) {
-    Type indexType = converter.getIndexType();
-    Value offsetVal =
-        ShapedType::isDynamic(offsetCst)
-            ? offset(builder, loc)
-            : createIndexAttrConstant(builder, loc, indexType, offsetCst);
-    Type elementType = converter.convertType(type.getElementType());
-    ptr = builder.create<LLVM::GEPOp>(loc, ptr.getType(), elementType, ptr,
-                                      offsetVal);
-  }
+  // For zero offsets, we already have the base pointer.
+  if (offsetCst == 0)
+    return ptr;
+
+  // Otherwise add the offset to the aligned base.
+  Type indexType = converter.getIndexType();
+  Value offsetVal =
+      ShapedType::isDynamic(offsetCst)
+          ? offset(builder, loc)
+          : createIndexAttrConstant(builder, loc, indexType, offsetCst);
+  Type elementType = converter.convertType(type.getElementType());
+  ptr = builder.create<LLVM::GEPOp>(loc, ptr.getType(), elementType, ptr,
+                                    offsetVal);
   return ptr;
 }