[PATCH] D135262: [SimplifyLibCalls] Pre-commit test case showing bug with wide char support

Bjorn Pettersson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 5 05:44:38 PDT 2022


bjope created this revision.
bjope added a reviewer: MatzeB.
Herald added a project: All.
bjope requested review of this revision.
Herald added a project: LLVM.

The ValueTracking support for getting the string length of a wchar_t
string (e.g. using wcslen) seem to be having some bugs.

Problem I've seen is that llvm::getConstantDataArrayInfo is taking
both a "ElementSize" argument (basically indicating size of a
char/element in bits) and an "Offset" which afaict is an offset
in the unit "number of elements". Then it also use
stripAndAccumulateConstantOffsets to get a "StartIdx" which afaict
is calculated in bytes. The returned Slice.Length is based on
arithmetics that add/subtract variables that are having different
units (bytes vs elements). Most notably I think the "StartIdx" must
be scaled using the "ElementSize" to get correct results.

This patch just adds a new test case showing that we get a wrong
result when doing wcslen(x + c). The actual fix to the above problem
will be done in a follow up commit.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135262

Files:
  llvm/test/Transforms/InstCombine/wcslen-7.ll


Index: llvm/test/Transforms/InstCombine/wcslen-7.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/wcslen-7.ll
@@ -0,0 +1,23 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+;
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+declare i64 @wcslen(ptr)
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!llvm.module.flags = !{!0}
+
+ at ws = constant [10 x i32] [i32 9, i32 8, i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0]
+
+
+; Fold wcslen(ws + 2) => 7.
+; FIXME: This fold is faulty, result should be 7 not 1.
+define dso_local i64 @fold_wcslen_1() {
+; CHECK-LABEL: @fold_wcslen_1(
+; CHECK-NEXT:    ret i64 1
+;
+  %ps3_pi = getelementptr inbounds [10 x i32], ptr @ws, i64 0, i64 2
+  %len = tail call i64 @wcslen(ptr %ps3_pi)
+  ret i64 %len
+}
+


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135262.465356.patch
Type: text/x-patch
Size: 858 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221005/947a664c/attachment.bin>


More information about the llvm-commits mailing list