Skip to content

Instantly share code, notes, and snippets.

@kritgpt
Created March 4, 2023 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kritgpt/69bfc7ef78e1784716c693d531a58adf to your computer and use it in GitHub Desktop.
Save kritgpt/69bfc7ef78e1784716c693d531a58adf to your computer and use it in GitHub Desktop.
Python script to remove occurences of 'br undef' in LLVM tests
# imports for input and regex
import sys
import re
# open each file passed as argument in a loop
for filename in sys.argv[1:]:
globalCount = 0 # used for numbering replacements in comments
filebuffer = [] # stores all lines before writing
localbuffer = []
flag1 = 0
brCount = 0
prevLine = ''
# open and read the file
f = open(filename, 'r')
for line in f:
if "define" in line:
if flag1:
# change the function signature of the previous function to include the new arguments
pattern = re.compile("define(?P<returnType>.*)@(?P<functionName>.*)\((?P<args>.*)\)")
m = pattern.match(prevLine)
if(m != None):
comma = "," if m.group('args') != "" else ""
if(brCount > 0):
prevLine = re.sub("define (?P<returnType>.*) @(?P<functionName>.*)\((?P<args>.*)\)", "define \g<returnType> @\g<functionName>(\g<args>"+comma+" %s)" % ', '.join([r'i1 %replaceUndef'+str(x) for x in range(brCount)]),prevLine)
filebuffer.append(prevLine)
for b in localbuffer:
filebuffer.append(b)
prevLine = line
brCount = 0
localbuffer = []
flag1 = 1
else:
# replace undef with a new variable in br statements
if re.match('.*br.*undef',line):
if re.match('^;',line.strip()):
# replace undef in comments
line = re.sub('undef', r'[[%replaceUndef'+str(globalCount)+':%.*]]' ,line)
globalCount+=1
continue
line = re.sub('undef', r'%replaceUndef'+str(brCount) ,line)
brCount += 1
localbuffer.append(line)
if flag1:
# change the function signature of the last function to include the new arguments
pattern = re.compile("define(?P<returnType>.*)@(?P<functionName>.*)\((?P<args>.*)\)")
m = pattern.match(prevLine)
if(m != None):
comma = "," if m.group('args') != "" else ""
if(brCount > 0):
prevLine = re.sub("define (?P<returnType>.*) @(?P<functionName>.*)\((?P<args>.*)\)", "define \g<returnType> @\g<functionName>(\g<args>"+comma+" %s)" % ', '.join([r'i1 %replaceUndef'+str(x) for x in range(brCount)]),prevLine)
filebuffer.append(prevLine)
for b in localbuffer:
filebuffer.append(b)
f.close()
# write the file
f_out = open(filename, 'w')
f_out.writelines(filebuffer)
f_out.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment