def add_rollup(raw, tag):
    """
    Returns a buffer that has ' R' appended to all performance report
    lines in the text buffer raw that are tagged with string tag.
    """
    result = []
    for line in raw.split('\n'):
        words = line.split('[ PERFORMANCE ] ')
        if len(words) == 2 and words[0] == '' and words[1].startswith(tag + ' ') and not line.endswith(' R'):
            line = line + ' R'
        result.append(line)
    result = '\n'.join(result)
    return result